1

I am developing an Android app. In my app I need to convert file into byte array. I tried a Stack Overflow solution and it is always giving me null.

This is my onActivityResult callback:

        Uri bookUri = data.getData();
        if(bookUri!=null)
        {
            String filePath = bookUri.toString();
            String mime = app.getMimeType(filePath);
            if(mime!=null && !mime.isEmpty() && (mime.toLowerCase()=="application/pdf" || mime.toLowerCase()=="application/txt" || mime.toLowerCase()=="application/text"))
            {
                bookFile = new File(filePath);
                if(bookFile!=null)
                {
                    byte[] bookByteArray = app.convertFileToByteArray(bookFile); //Converting file to byte array here
                    if(bookByteArray==null)
                    {

                        Toast.makeText(getBaseContext(),"NULL",Toast.LENGTH_SHORT).show();
                    }

                }
                //change book cover image
                ivBookFile.setImageResource(R.drawable.book_selected);
            }
            else{
                Toast.makeText(getBaseContext(),"Unable to process file you have chosen.",Toast.LENGTH_SHORT).show();
            }
        }

I commented where I am converting file to byte array in above code. The above code always toasting "NULL" message.

This is my convert method

public byte[] convertFileToByteArray(File file)
    {
        FileInputStream fileInputStream=null;

        byte[] bFile = new byte[(int) file.length()];

        try {
            //convert file into array of bytes
            fileInputStream = new FileInputStream(file);
            fileInputStream.read(bFile);
            fileInputStream.close();

            return bFile;
        }catch(Exception e){
           return null;
        }
    }

Why it is always null? How can I correctly convert file to byte array in Android?

halfer
  • 19,824
  • 17
  • 99
  • 186
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372
  • Possible duplicate? [File to byte array](http://stackoverflow.com/questions/858980/file-to-byte-in-java) – S.V. Aug 19 '16 at 10:03
  • Most likely you caught an exception. Log the exception to find out what's wrong. – Henry Aug 19 '16 at 10:14
  • I got this error - /file:/storage/emulated/0/Download/147125382649668.pdf: open failed: ENOENT (No such file or directory) . "file:///storage" is auto converted into "file:/storage" . Please how can I solve the problem @Henry – Wai Yan Hein Aug 19 '16 at 10:50
  • Is there really a `/` in before `file:`? this should not be there. Also, does the file really exist at that place and does your process have access rights? Moreover, the `File` constructor expects a file name not an URL. – Henry Aug 19 '16 at 10:52
  • Yes. Here is the full log EXCEPTION_SMS: /file:/storage/emulated/0/Download/147125382649668.pdf: open failed: ENOENT (No such file or directory) – Wai Yan Hein Aug 19 '16 at 11:01

1 Answers1

0
 public static byte[] readBytes(InputStream inputStream) throws IOException {
    byte[] b = new byte[1024];
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    int c;
    while ((c = inputStream.read(b)) != -1) {
      os.write(b, 0, c);
    }
    return os.toByteArray();
  }
Ramit
  • 416
  • 3
  • 8