我有下一個問題...我搜索的來源答案,但沒有找到一個很好的答案...在Android中保存文件:java.io.FileNotFoundException。打開失敗:ENOENT(沒有這樣的文件或目錄)
java.io.FileNotFoundException: /mounted/EmailClient/side-corner.png: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:453)
at java.io.FileOutputStream.<init>(FileOutputStream.
at java.io.FileOutputStream.<init>(FileOutputStream.java:73) etc
代碼:
private static String saveFile(String filename, InputStream input) {
String path = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(path)) {
try {
byte[] attachment = new byte[input.available()];
input.read(attachment);
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/EmailClient/", filename);
if (!file.mkdirs())
Log.d("EmailClient", "saveFile: Dir not created");
FileOutputStream out = new FileOutputStream(file); //PROBLEM!
out.write(attachment);
input.close();
out.close();
return path;
} catch (IOException e) {
Log.e("EmailClient", "saveFile: File not saved", e);;
}
}
return path;
}
文件夾和文件被創建,但文件的大小= 0 mkdirs()和createNewFile()返回false ... – Dzmitry
但根據例外情況,找不到錯誤文件 – LearnPainLess
但byte [] attachment = [B @ 428943b8 – Dzmitry