简体   繁体   中英

FileNotFoundException while trying to access external storage Android

I am following the code sample at Android Developer guide for capturing video from my app. Things are pretty smooth until I invoke the prepare method on MediaRecorder instance. It throws an IOException with following message:

java.io.FileNotFoundException: /file:/storage/emulated/0/Pictures/MyApp/MyVideoFile.mp4: 
open failed: ENOENT (No such file or directory)

The code is pretty much the same as on the site. To summarize, there is:

  • CameraActivity class for managing the Camera and MediaRecorder resources.

  • FileManager class that returns a Uri for output file path to be used for storing the video preview frames.

Code snippet:

public static File getOutputMediaFile(int type) throws Exception {
        if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            throw new Exception("Unable to access device SDCard.");
        }

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "MyApp");

    if(! mediaStorageDir.exists()) {
        if(! mediaStorageDir.mkdirs()) {
            Log.d(CameraActivity.MYAPP, "Failed to create directory.");
        }
    }

    // Create a media file name.
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath(),
        "MyAudioFile.jpg");
    } else if(type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath(),
        "MyVideoFile.mp4");
    } else {
        return null;
    }

    return mediaFile;
}

The mediaFile is passed as an argument to _mediaRecorder.setOutputFile() method. However, when I invoke _mediaRecorder.prepare() , it throws the IOException .

I have already included <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> in AndroidManifest file.

The surprising point is that if I omit the mediaRecorder.prepare call and check the sdcard for presence of the file, it is there !! However, if I run the above code to completion and check back the folder, the file is not there. It appears the file is getting deleted by _mediaRecorder.prepare call !!

I am pretty new to Android development and have already spent over a day trying to figure this out. Any help will be greatly valued !!

I've followed this http://developer.android.com/guide/topics/media/camera.html#capture-video example and had the same issue.

The problem there, was that the method to create the directory and construct the file name returned a File but later wrapped by another method which returned a Uri. The Uri added the "/file:" to the file path string. Passing the string returned by the URI to mediaRecorder.setOutputFile(..), made the prepare function call to throw an exception.

I fixed it by simply taking the video file and calling ToString() to get its path and pass it to mediaRecorder.setOutputFile(..); Then prepare() worked

If you are using emulator increase the SD Card space to 1024mb. Also check if you are affected by this one: WRITE_MEDIA_STORAGE http://www.chainfire.eu/articles/113/Is_Google_blocking_apps_writing_to_SD_cards_/

Other: https://android.googlesource.com/platform/packages/providers/MediaProvider/+/android-4.1.2_r2.1/AndroidManifest.xml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM