该代码在大多数情况下都能正常工作,但有时会抛出异常。无法弄清楚是什么原因造成的。为什么有时会抛出FileNotFoundException
什么是确实是在
/storage/emulated/0/Download/theFileName.jpg
创建文件和写入数据到它(从它的资源文件确实存在),但新创建的文件得到“文件不存在”异常。
(它确实在明显有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); – phongvan
Lispas,该应用程序已运行。 mdtuyen,File newFile = new File(downlaodDirectory,fileName);没有帮助,并且如果同时具有downlaodDirectory和fileName,它与新文件(downlaodDirectory.getPath()+“/”+ fileName)相同; – lannyf