简体   繁体   English

Android 应用程序在定义字节缓冲区以读取文件时崩溃

[英]Android application crashes while defining byte buffer to read a file

I am trying to read a file into a byte buffer in android.我正在尝试将文件读入 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.我已经正确检查并且文件大小远低于 int 最大值。 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.由于某些项目设置,我必须在设备上测试应用程序,因为我无权访问 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.谢谢。

Use try catch block and display error with toast.使用 try catch 块并在 toast 中显示错误。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM