我一直在努力将ArrayList存储在Galaxy S4的SD卡上。由于我似乎无法自己解决的原因,我在这段代码中不断获得File Not Found Exception:
File file = new File(Environment.getExternalStorageDirectory(), name);
if(file.exists()) file.delete();
try{
OutputStream os = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(os);
for(int j = 0; j < fallList.size(); j++)
osw.write(fallList.get(j));
os.close();
osw.close();
} catch (FileNotFoundException e){
Toast.makeText(getApplicationContext(),"File Not Found",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
编辑:
正确添加权限后,我不再获得File Not Found Exception,而是IOException。我注意到设备监视器中存在一些系统错误:
04-26 13:54:15.115:D / ViewRootImpl(27953):ViewPostImeInputStage ACTION_DOWN 04-26 13:54:20.130:D / ViewRootImpl(27953):ViewPostImeInputStage ACTION_DOWN 04-26 13:54:20.270:W / System.err(27953):java.io.IOException:文件描述符已关闭 04-26 13:54:20.280:W / System.err(27953):at libcore.io.Posix.writeBytes(Native Method) 04-26 13:54:20.310:W / System.err(27953):at libcore.io.Posix.write(Posix.java:223) 04-26 13:54:20.310:W / System.err(27953):at libcore.io.BlockGuardOs.write(BlockGuardOs.java:313) 04-26 13:54:20.310:W / System.err(27953):at libcore.io.IoBridge.write(IoBridge.java:497) 04-26 13:54:20.310:W / System.err(27953):at java.io.FileOutputStream.write(FileOutputStream.java:186) 04-26 13:54:20.310:W / System.err(27953):at java.io.OutputStreamWriter.flushBytes(OutputStreamWriter.java:167) 04-26 13:54:20.310:W / System.err(27953):at java.io.OutputStreamWriter.close(OutputStreamWriter.java:140) 04-26 13:54:20.310:W / System.err(27953):at com.example.med603.sharedpreffalldetect.MainActivity.toFile(MainActivity.java:142) 04-26 13:54:20.310:W / System.err(27953):at com.example.med603.sharedpreffalldetect.MainActivity.access $ 000(MainActivity.java:35) 04-26 13:54:20.310:W / System.err(27953):at com.example.med603.sharedpreffalldetect.MainActivity $ 2.onClick(MainActivity.java:73) 04-26 13:54:20.310:W / System.err(27953):在android.view.View.performClick(View.java:5197) 04-26 13:54:20.310:W / System.err(27953):在android.view.View $ PerformClick.run(View.java:20926) 04-26 13:54:20.310:W / System.err(27953):在android.os.Handler.handleCallback(Handler.java:739) 04-26 13:54:20.310:W / System.err(27953):在android.os.Handler.dispatchMessage(Handler.java:95) 04-26 13:54:20.310:W / System.err(27953):在android.os.Looper.loop(Looper.java:145) 04-26 13:54:20.310:W / System.err(27953):在android.app.ActivityThread.main(ActivityThread.java:5951) 04-26 13:54:20.320:W / System.err(27953):at java.lang.reflect.Method.invoke(Native Method) 04-26 13:54:20.320:W / System.err(27953):at java.lang.reflect.Method.invoke(Method.java:372) 04-26 13:54:20.320:W / System.err(27953):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1400) 04-26 13:54:20.320:W / System.err(27953):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
之前我还没有使用过d.log,所以我不完全确定我在这里看到的是什么。
答案 0 :(得分:0)
答案 1 :(得分:0)
所以我明白了;首先,当我“重新做”应用程序时,我忘记了权限(第一次尝试太乱了) - 这解决了文件未找到异常!
然后我得到了一个IOException,这似乎是因为我没有刷新OutputStreamWriter。