Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why sometime it throws FileNotFoundException

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) {}
}
like image 235
lannyf Avatar asked Nov 30 '15 14:11

lannyf


People also ask

How do I fix FileNotFoundException?

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 do I stop FileNotFoundException in java?

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.

In which cases FileNotFoundException at compile-time will be thrown?

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.

What cases are in FileNotFoundException?

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.


1 Answers

I can think of the following possible reasons for this behavior:

  1. The new file could not be created simply because there is no free space on SD card. The next time you encounter this issue, try to see if you have at least some available space via file manager or Settings -> Storage. I doubt you can do something about it if this is what happens on your user's device.
  2. The SD card is not available due to some OS glitch or it's physically not attached, thus the file could not be created. Once again - there is nothing you can do about it, other than showing some Toast with error message.
  3. How do you generate the 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.
  4. 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();
    }
    
  5. The 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.
  6. 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) { }
        }
    }
    
  7. The last one - and finally I'm out of options. :) I suppose this issue could also arise if you write to the same file from multiple threads. If that's the case, try to synchronize access to your file-writing code.
like image 90
aga Avatar answered Oct 08 '22 13:10

aga