0

I am trying to read a file into a byte buffer in android. The application crashes whenever I initialize the byte buffer with the size equal to the size of the file. I have checked correctly and the file size is well below the int max value. Because of certain project setup, I have to test the application on the device due to which I don't have the access to the logcat.

File outputdir = new File(localcontext.getFilesDir(), "appData");
                if(!outputdir.exists()){
                    if(outputdir.mkdir()){
                        Toast.makeText(localcontext, outputdir.getAbsolutePath(), Toast.LENGTH_SHORT).show();
                    }
                }

                tempfile = new File(outputdir, "runningfile.mp4");

                bytebuffer = new byte[(int)encryptedfile.length()];
                OutputStream os = new FileOutputStream(tempfile.getAbsolutePath(), false);

//                DataInputStream dataInputStream = new DataInputStream(fis);
//                dataInputStream.readFully(bytebuffer);
//                dataInputStream.close();

The application runs fine and displays some message when I comment out the byte buffer initialization line but crashes otherwise. I am unable to figure out what's wrong here. Please help. Thanks.

Kumar Gaurav
  • 1
  • 1
  • 3
  • 1
    Use Logcat to examine the stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this Most likely, you do not have a free memory block big enough for that MP4 file. – CommonsWare Sep 28 '19 at 19:11

1 Answers1

0

Use try catch block and display error with toast.

try{
    bytebuffer = new byte[(int)encryptedfile.length()];
}catch(Exception e){
  Toast.makeText(getActivity(), e.getMessage(),Toast.LENGTH_LONG).show();
}
oto
  • 383
  • 4
  • 18