创建文件到流输出后的FileNotFoundException

时间:2015-06-09 00:24:07

标签: java android filestream crashlytics

我最近实施了Crashlytics并且正在通过一个真正的崩溃日志聚宝盆走路。一个人让我有点困惑。在日志中显示我无法打开要传输的文件(特别是写一张我拍摄的JPG照片),但是在此之前的一行(相信)我成功创建了它。来自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)

这是源代码:

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
            }
        }
    }
}

我的AndroidManifest.xml的使用权限是:

<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" />

例外情况发生在outStream = new FileOutputStream(file);但是当我尝试创建文件本身来传输输出时,我希望它有一个错误。

可能遗漏了一些非常明显的东西......

2 个答案:

答案 0 :(得分:0)

我不确定您所指的目录是否为app-private。我遇到了类似的问题并通过修复max-sdk参数解决了这个问题。 您可以在此处查找“保存app-private文件”部分: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

答案 1 :(得分:0)

输出文件上的

FileNotFoundException表示找不到目录