我将content://com.android.externalstorage.documents/tree/
路径设置为MediaRecorder的输出文件
这是SD卡中的路径
DocumentFile videoDocFile = getOutputMediaDocFile(MEDIA_TYPE_VIDEO, mContext);
//videoDocFile.toString() is content://com.android.externalstorage.documents/tree/4AEA-D5ED%3ADownload/document/4AEA-D5ED%3ADownload%2FMP4_20170922_134638.mp4
mMediaRecorder.setOutputFile(videoDocFile.toString());
private static DocumentFile getOutputMediaDocFile(int type, Activity mContext){
//customPath value is content://com.android.externalstorage.documents/tree/4AEA-D5ED%3ADownload
String customPath=PreferenceCenter.getCustomPath(mContext);
Uri myuri = Uri.parse(customPath);
DocumentFile pickedDir = DocumentFile.fromTreeUri(mContext, myuri);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
DocumentFile mediaFile;
mediaFile = pickedDir.createFile("video/mp4", "MP4_"+ timeStamp + ".mp4");
return mediaFile;
}
当我打电话准备
mMediaRecorder.prepare();
我得到了这个例外
android.support.v4.provider.TreeDocumentFile@3ed6c56: open failed: EROFS (Read-only file system)
如果我去SD卡,我可以看到mp4文件,但文件是0字节
所以路径是正确的,但我无法将内容写入文件
如何使用MediaRecorder将视频文件保存到content://com.android.externalstorage.documents/tree/
?
如果您创建一个名为DocumentFile
的新file
并使用ContentResolver openFileDescriptor(uri, "rwt")
并获取ParcelableFileDescriptor
,请将其命名为:pfd
并调用pfd.getFileDescriptor()
以获取名为:fd
的java FileDescriptor。然后,setOputputFile(fd)
。