FileNotFoundException after file created to stream output

643 views Asked by At

I recently implemented Crashlytics and am walking thru a veritable cornucopia of crash logs. One has me a bit puzzled. In the log is shows that I could not open a file to stream to (specifically to write a JPG photo I took), however one line before that I (believe) I successfully created it. The exception(s) from Crashlytics:

Non-fatal Exception: java.io.FileNotFoundException
/storage/emulated/0/mycompany/9311ebcb-4dd5-493c-8fbd-46b242d4991d/b9a9a18e-0cd9-468d-83f3-7466b65485f7.jpg: open failed: ENOENT (No such file or directory)
 raw
libcore.io.IoBridge.open (IoBridge.java:409)
java.io.FileOutputStream.<init> (FileOutputStream.java:73)
com.invocore.mycompany.utility.Utilities.saveImageJPEG (Utilities.java:698)
com.invocore.mycompany.utility.Utilities.saveImageJPEG (Utilities.java:685)
com.invocore.mycompany.utility.Utilities.saveImage (Utilities.java:634)
com.invocore.mycompany.CameraCaptureActivity.savePhoto (CameraCaptureActivity.java:338)
com.invocore.mycompany.CameraCaptureActivity.access$300 (CameraCaptureActivity.java:39)
com.invocore.mycompany.CameraCaptureActivity$SaveTask.doInBackground (CameraCaptureActivity.java:266)
com.invocore.mycompany.CameraCaptureActivity$SaveTask.doInBackground (CameraCaptureActivity.java:246)
android.os.AsyncTask$2.call (AsyncTask.java:288)
java.lang.Thread.run (Thread.java:841)
Caused by libcore.io.ErrnoException
libcore.io.Posix.open (Posix.java)
java.io.FileOutputStream.<init> (FileOutputStream.java:73)
com.invocore.mycompany.utility.Utilities.saveImageJPEG (Utilities.java:698)
com.invocore.mycompany.utility.Utilities.saveImageJPEG (Utilities.java:685)
com.invocore.mycompany.utility.Utilities.saveImage (Utilities.java:634)
com.invocore.mycompany.CameraCaptureActivity.savePhoto (CameraCaptureActivity.java:338)
com.invocore.mycompany.CameraCaptureActivity.access$300 (CameraCaptureActivity.java:39)
com.invocore.mycompany.CameraCaptureActivity$SaveTask.doInBackground (CameraCaptureActivity.java:266)
com.invocore.mycompany.CameraCaptureActivity$SaveTask.doInBackground (CameraCaptureActivity.java:246)
android.os.AsyncTask$2.call (AsyncTask.java:288)
java.lang.Thread.run (Thread.java:841)

Here's the source code:

public static void saveImageJPEG(Bitmap image, int width, int height, String filename, String directory, int quality)
{
    Bitmap scaled = Bitmap.createScaledBitmap(image, width, height, true);
    OutputStream outStream = null;
    // Only add extension if we need to
    if (!filename.toLowerCase().endsWith(".jpg")) {
        filename = filename + ".jpg";
    }

    try {
        File file = new File(directory, filename);
        outStream = new FileOutputStream(file);
        scaled.compress(Bitmap.CompressFormat.JPEG, quality, outStream);
    }
    catch(Exception e) {
        Utilities.logException(e);
        e.printStackTrace();
    }
    finally {
        if (outStream != null) {
            try {
                outStream.flush();
                outStream.close();
            }
            catch (Exception ex) {
                // Ignore for now
            }
        }
    }
}

My uses-permissions from my AndroidManifest.xml are:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.NFC" />

The exception is happening at outStream = new FileOutputStream(file); but I would expect it to have had an error when I attempted to create the file itself in which to stream the output.

Probably missing something very obvious...

2

There are 2 answers

0
Debosmit Ray On

I am not sure if the directory you are referring to is app-private or not. I encountered a similar issue and solved it by fixing the max-sdk parameter. You can look for the section "Saving files that are app-private" here: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

1
user207421 On

FileNotFoundException on an output file means a directory wasn't found.