[英]Prevent app from crashing from java.io.IOException: write failed: ENOSPC (No space left on device)
In an attempt to write to the device, when the device is full, I am getting error: com.google.gson.JsonIOException: java.io.IOException: write failed: ENOSPC (No space left on device)
.在尝试写入设备时,当设备已满时,出现错误: com.google.gson.JsonIOException: java.io.IOException: write failed: ENOSPC (No space left on device)
。 I have a try-catch block to catch an IOException and a few lines to check the size of the list and storage space available to determine if there is space to write the file but yet it still crashed the app and I got the IOException mentioned above.我有一个 try-catch 块来捕获 IOException 和几行来检查列表的大小和可用的存储空间以确定是否有空间来写入文件,但它仍然使应用程序崩溃,我得到了上面提到的 IOException .
Here is the code:这是代码:
File file = new File(getExternalFilesDir(null), fileName+".txt");
int file_size = Integer.parseInt(String.valueOf(file.length()/1024));
long storage_size = (getAvailableInternalMemorySize()/1024);
long list_size = listToWrite.toString().getBytes().length/1024;
long total_size = file_size + list_size;
if(total_size < storage_size) {
try (Writer writer = new FileWriter(file, false)) {
Gson gson = new GsonBuilder().create();
gson.toJson(listToWrite, writer);
} catch (IOException e) {
e.printStackTrace();
}
}
Is there a way to stop the app from crashing and just display a Toast message to the user or perform some action.有没有办法阻止应用程序崩溃并只向用户显示 Toast 消息或执行某些操作。
在您的清单文件中添加此属性:
android:installLocation="preferExternal"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.