我将content://com.android.externalstorage.documents/tree/
路径设置为输出文件
这是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/
?
如果创建一个名为 file
的新DocumentFile
并使用ContentResolver openFileDescriptor(uri, "rwt")
并获取ParcelableFileDescriptor
,命名为:pfd
并调用pfd.getFileDescriptor()
以获取名为:fd
的Java filescriptor。然后,setOputputFile(fd)
。