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

  • Map tiles flicker between terrain and normal types
  • Silent installation on Android devices
  • How can port AsyncTask from Android to java?
  • How to disable/enable all children on LinearLayout in Android
  • Determine if Activity came to front due to back navigation
  • Android Lock Apps
  • /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) {}

    Related posts:

    Android : Cannot complete the install because of a conflicting dependency
    What is going on with my Android Navigation drawer activity?
    Why Would Dependencies Be Included Only In Release Builds?
    Can an Android Toast be longer than Toast.LENGTH_LONG?
    Android: How to set icon in title bar of Dialog activity?
    What length is iOS and Androids device token
  • Is the onUpgrade method ever called?
  • A boolean value defined in XML. How to reference in Java?
  • How to read window content (using accessibilityService) and evoking UI using draw over other app permission in Android?
  • Android create a spinner with items that have a hidden value and display some text?
  • Flash inside Android WebView — How do I set fullscreen automatically?
  • Call .NET web service in Android
  • 5 Solutions collect form web for “why sometime it throws FileNotFoundException”

    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()) {
    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 {
              } catch (Exception e) { }
          if (out != null) {
              try {
              } 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.

    According to Java docs – Class FileNotFoundException:

    public class FileNotFoundException extends IOException

    Signals that an attempt to open the file denoted by a specified pathname has failed.

    This exception will be thrown by the FileInputStream,
    FileOutputStream, and RandomAccessFile constructors when a file with
    the specified pathname does not exist. It will also be thrown by these
    constructors if the file does exist but for some reason is
    inaccessible, for example when an attempt is made to open a read-only
    file for writing.

    To resume, there are three cases where a FileNotFoundException may be thrown.

    1. The named file does not exist.
    2. The named file is actually a directory.
    3. The named file cannot be opened for reading for some reason.

    got “file not exist” exception for the newly created file.

    In your code I don’t see two things

    1. Removing file after successful write. I’m not kidding at all. If I had a class like this and that error, I would make a kind of test, which would finish with deleting downlaodDirectory after copying content from source file. In that case it would pass the method or you would get another, but more specific error – like in my words: unable to delete file. Under use it
    2. Closing file after writing data. I don’t see also a method to close files. In your code, does seem to be that your File is still open, so you run again an app and you get this unpredicted by you error.

    To fix the issue just close both files after processing data using file.close() method.

    Hope it help

    1. The issue might happen due to the way you create file:

      String downloadPath = downloadDirectory.getPath();
      String newFilePath = (downloadPath + "/" + fileName);
      File newFile = new File(newFilePath);

      The dangerous code is here:

      String newFilePath = (downloadPath + "/" + fileName);

      Instead, try to use the following approach:

      File newFile = new File(downloadDirectory, fileName);
    2. SD card availability – check the following documentation
      It might be that media is not mounted (for instance on some old devices
      it happens when user connects device to computer via usb) So, try to
      check if media mounted:

      /* Checks if external storage is available for read and write */
      public boolean isExternalStorageWritable() {
           String state = Environment.getExternalStorageState();
           if (Environment.MEDIA_MOUNTED.equals(state)) {
               return true;
           return false;

      and notify user if not.

    you didn’t posted full exception stack traces but I found exact problem solved by SO.

    java.io.FileNotFoundException: /storage/emulated/0/bimagechooser/1429031333305.jpg: open failed: ENOENT (No such file or directory)

    Use this library image-chooser-library. & need to re-factor code.

    The Guy coomar2841. will surely help you. as he spend hours solving the problem. so save your time.

    Took the sources of com.kbeanie.imagechooser and dropped them into your APP, removed the gradle declaration for the library and the APP should work.

    please view the GitHub Issue

    I hope it’ll work for you. 🙂

    I hope your code to write data in file is perfect I am just Concentrate in your problem.I think there is two possibility to get File not found Exception.

    1. Some time File Name is not Support Some Special Character While you provide this kind of character to giving file name at that time file is not properly Generated that’s why this error was occur So please Make sure your file name is Proper.
    2. second is Your file extension.here your not specified which kind of file you have.

    here I have Some Code Snippet I hope this will helping you to Solve problem.


       File file=new File(Environment.getExternalStorageDirectory()+"/"+ConstansClass.FOLDERNAME);
        String file=filename.replaceAll("[^a-zA-Z0-9.-]", " ");
        yourDir = new File(file, file+".mp3");
            return yourDir;


    I am Sorry lannf I don’t know what exactly happen in your code but i have one code snippet which is just download mp3 file from soundcloude and Store in my local storage.i think it’s exactly same as your requirement.so i have post my code below.

    protected File doInBackground(Void... params) {
        //File sdCardRoot = Environment.getExternalStorageDirectory()+"/Music";
        File file=new File(Environment.getExternalStorageDirectory()+"/"+ConstansClass.FOLDERNAME);
        String filename=soundCloudeResponse.getTitle();//.replaceAll("[^a-zA-Z0-9.-]", " ");
        yourDir = new File(file, filename+".mp3");
            return yourDir;
        String url=soundCloudeResponse.getStream_url()+"?"+ConstansClass.SOUNDCLOUDE_CLIENT_ID;
        URL u = null;
        try {
            DebugLog.e("Request Url"+ url);
            u = new URL(url);
            URLConnection conn = u.openConnection();
            int contentLength = conn.getContentLength();
            DataInputStream stream = new DataInputStream(u.openStream());
            byte[] buffer = new byte[contentLength];
            DataOutputStream fos = new DataOutputStream(new FileOutputStream(yourDir));
            DebugLog.d("Download Complete in On Background");
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        catch (Exception e)
            DebugLog.e("Error ::"+e.getMessage());
        return yourDir;

    I think your are intelligent developer so you can modify this code according to your requirement.

    Best Of Luck

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.