简体   繁体   English

防止应用程序从 java.io.IOException 崩溃:写入失败:ENOSPC(设备上没有剩余空间)

[英]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.

相关问题 运行sbt失败 - java.io.IOException:设备上没有剩余空间 - Running sbt fails - java.io.IOException: No space left on device 如何避免这种java.io.IOException:设备上没有剩余空间 - How to avoid this java.io.IOException: No space left on device java.io.IOException:设备Android上没有剩余空间 - java.io.IOException: No space left on device Android Debian上的PHPStorm EAP更新:java.io.IOException:设备上没有剩余空间 - PHPStorm EAP update on Debian: java.io.IOException: No space left on device 任务 &#39;:app:compileDebugKotlin&#39; java.io.IOException 执行失败 - Execution failed for task ':app:compileDebugKotlin' java.io.IOException 索引器:java.io.IOException:作业失败 - Indexer: java.io.IOException: Job failed java.io.IOException:grpc 失败 - java.io.IOException: grpc failed 从 java.io.IOException 中提取 HTTP 状态码 - Extract HTTP Status Code from java.io.IOException 从错误响应中获取数据时出现 java.io.IOException - java.io.IOException when getting data from error response 具有拍摄意图的Android FileProvider给出java.io.IOException:写入失败:EPIPE(管道损坏) - Android FileProvider with photo take intent gives a java.io.IOException: write failed: EPIPE (Broken pipe)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM