The code works for most of the time, but some time it throws exception. Couldn't figure out what could cause it.
What is does is to create a file at
/storage/emulated/0/Download/theFileName.jpg
and write data to it (from sourceFile which does exist), but got "file not exist" exception for the newly created file.
(it does have uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE", and uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE
" in manifest).
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) {}
}
How to Fix FileNotFoundException. Since FileNotFoundException is a checked exception, a try-catch block should be used to handle it. The try block should contain the lines of code that can throw the exception and the catch block should catch and handle the exception appropriately.
How to avoid FileNotFoundException in java? Please ensure file exists when you try to read from file from path exists (using FileInputStream) to avoid FileNotFoundException. Please ensure file exists when we try to acces file from invalid path using RandomAccessFile to avoid FileNotFoundException.
FileNotFoundException is a checked exception is used that occurs when a file path specified for accessing does not exist or is inaccessible. With the checked exception, it means that the java compiler checks at compile time if this exception has been handled or not; otherwise, a compile-time error occurs.
FileNotFoundException is thrown by constructors of FileInputStream, FileOutputStream, RandomAccessFile when file is not found on specified path. Exception can also be raised when file is inaccessible for some reason. For example: When you do not have proper permissions to read the files.
I can think of the following possible reasons for this behavior:
Toast
with error message.fileName
part of your file's path? Sometimes the files can not be created because the filename contains unsupported (by this particular device) characters. E.g. I have HTC Desire X with Android 4.1.2 which can not create files with the code similar to yours if the filename contains colon. Try another way to generate the filename and see if it makes a difference.Did you make sure that the Downloads
directory exists before creating the file inside it? Write something like the following:
if (!downloadDirectory.exists()) {
downloadDirectory.mkdirs();
}
WRITE_EXTERNAL_STORAGE
permission is considered dangerous on the Android 6, thus it can be revoked by the user at any time. Make sure user didn't revoke this permission before writing to the file.Always close the I/O stream when you finished working with it. Add the finally
clause to your try-catch block and close both in
and out
streams in it:
finally {
if (in != null) {
try {
in.close();
} catch (Exception e) { }
}
if (out != null) {
try {
out.close();
} catch (Exception e) { }
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With