【发布时间】:2016-03-04 05:19:54
【问题描述】:
代码大部分时间都能正常工作,但有时会抛出异常。不知道是什么原因造成的。
要做的是在
处创建一个文件/storage/emulated/0/Download/theFileName.jpg
并向其写入数据(来自确实存在的 sourceFile),但新创建的文件出现“文件不存在”异常。
(它确实有 uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE", and uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" 在清单中)。
File sourceFile = new File(theSourceFileFullPath);
if (sourceFile.exists()) {
File downloadDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
String downloadPath = downloadDirectory.getPath();
String newFilePath = (downloadPath + "/" + fileName);
File newFile = new File(newFilePath);
try {
FileInputStream in = new FileInputStream(sourceFile);
// ava.io.FileNotFoundException:
// /storage/emulated/0/Download/theFileName.jpg: open failed: ENOENT (No such file or directory)
// exception at this line
FileOutputStream out = new FileOutputStream(newFile);
//......
} catch (Exception e) {}
}
【问题讨论】:
-
可能没有完成它的创建,您尝试访问它。每次应用打开时,它都会尝试创建一个文件并从中读取?
-
尝试替换:File newFile = new File(downlaodDirectory, fileName);
-
Lispas,应用程序一直在运行。 mdtuyen, File newFile = new File(downlaodDirectory, fileName);没有帮助,如果同时具有 downlaodDirectory 和 fileName,则它与 new File(downlaodDirectory.getPath()+"/"+ fileName); 相同
-
FileInputStream 不创建文件。那么为什么会有这样的声明呢?你在做什么?从您的帖子中不清楚该异常是来自输入流还是输出流。
-
此错误是否出现在同一设备上?还是有些设备出错,有些设备没有?
标签: android filenotfoundexception fileoutputstream