[英]FileNotFoundException while trying to access external storage Android
I am following the code sample at Android Developer guide for capturing video from my app. 我正在关注Android开发人员指南中的代码示例,以便从我的应用中捕获视频 。 Things are pretty smooth until I invoke the prepare
method on MediaRecorder
instance. 在我调用MediaRecorder
实例上的prepare
方法之前,情况非常顺利。 It throws an IOException
with following message: 它抛出IOException
并显示以下消息:
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. 用于管理Camera和MediaRecorder资源的CameraActivity
类。
FileManager
class that returns a Uri for output file path to be used for storing the video preview frames. FileManager
类,返回用于存储视频预览帧的输出文件路径的Uri。
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. mediaFile
作为参数传递给_mediaRecorder.setOutputFile()
方法。 However, when I invoke _mediaRecorder.prepare()
, it throws the IOException
. 但是,当我调用_mediaRecorder.prepare()
,它会抛出IOException
。
I have already included <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in AndroidManifest file. 我已在AndroidManifest文件中包含<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
。
The surprising point is that if I omit the mediaRecorder.prepare
call and check the sdcard for presence of the file, it is there !! 令人惊讶的是,如果我省略了mediaRecorder.prepare
调用并检查sdcard是否存在该文件,它就在那里! 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 !! 看来文件被_mediaRecorder.prepare
调用删除!!
I am pretty new to Android development and have already spent over a day trying to figure this out. 我是Android开发的新手,已经花了一天多的时间试图解决这个问题。 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. 我已经按照http://developer.android.com/guide/topics/media/camera.html#capture-video示例进行了操作,并遇到了同样的问题。
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. 那里的问题是,创建目录和构造文件名的方法返回了一个文件,但后来由另一个返回Uri的方法包装。 The Uri added the "/file:" to the file path string. Uri将“/ file:”添加到文件路径字符串中。 Passing the string returned by the URI to mediaRecorder.setOutputFile(..), made the prepare function call to throw an exception. 将URI返回的字符串传递给mediaRecorder.setOutputFile(..),使prepare函数调用抛出异常。
I fixed it by simply taking the video file and calling ToString() to get its path and pass it to mediaRecorder.setOutputFile(..); 我通过简单地获取视频文件并调用ToString()来获取其路径并将其传递给mediaRecorder.setOutputFile(..); Then prepare() worked 然后prepare()工作了
If you are using emulator increase the SD Card space to 1024mb. 如果您使用的是模拟器,请将SD卡空间增加到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_/ 同时检查您是否受此影响: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 其他: https : //android.googlesource.com/platform/packages/providers/MediaProvider/+/android-4.1.2_r2.1/AndroidManifest.xml
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.