Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : How to read file in bytes?

Tags:

java

file

android

I am trying to get file content in bytes in Android application. I have get the file in SD card now want to get the selected file in bytes. I googled but no such success. Please help

Below is the code to get files with extension. Through this i get files and show in spinner. On file selection I want to get file in bytes.

private List<String> getListOfFiles(String path) {     File files = new File(path);     FileFilter filter = new FileFilter() {        private final List<String> exts = Arrays.asList("jpeg", "jpg", "png", "bmp", "gif","mp3");        public boolean accept(File pathname) {          String ext;          String path = pathname.getPath();          ext = path.substring(path.lastIndexOf(".") + 1);          return exts.contains(ext);       }    };     final File [] filesFound = files.listFiles(filter);    List<String> list = new ArrayList<String>();    if (filesFound != null && filesFound.length > 0) {       for (File file : filesFound) {          list.add(file.getName());       }    }    return list; } 
like image 253
Azhar Avatar asked Apr 06 '12 05:04

Azhar


People also ask

How do I convert files to bytes?

read(byte[]) method of FileInputStream class which reads up to the length of the file and then converts bytes of data from this input stream into the byte array. Procedure: Create an instance of File Input Stream with the file path. Create a byte array of the same length as the file.

How do I get bytes from Fileoutputstream?

To convert a file to byte array, ByteArrayOutputStream class is used. This class implements an output stream in which the data is written into a byte array. The buffer automatically grows as data is written to it. The data can be retrieved using toByteArray() and toString().

How to convert pdf File into byte array in Java?

This sample code shows PDF to byte array Java ConversionAdd(); // create Memory Stream var memoryStream = new System. IO. MemoryStream(); document. Save(memoryStream); // create Byte Array with PDF content var byteArray = memoryStream.


2 Answers

here it's a simple:

File file = new File(path); int size = (int) file.length(); byte[] bytes = new byte[size]; try {     BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));     buf.read(bytes, 0, bytes.length);     buf.close(); } catch (FileNotFoundException e) {     // TODO Auto-generated catch block     e.printStackTrace(); } catch (IOException e) {     // TODO Auto-generated catch block     e.printStackTrace(); } 

Add permission in manifest.xml:

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
like image 113
idiottiger Avatar answered Sep 22 '22 08:09

idiottiger


The easiest solution today is to used Apache common io :

http://commons.apache.org/proper/commons-io/javadocs/api-release/org/apache/commons/io/FileUtils.html#readFileToByteArray(java.io.File)

byte bytes[] = FileUtils.readFileToByteArray(photoFile) 

The only drawback is to add this dependency in your build.gradle app :

implementation 'commons-io:commons-io:2.5' 

+ 1562 Methods count

like image 24
Renaud Boulard Avatar answered Sep 23 '22 08:09

Renaud Boulard