如何使用MediaRecorder将视频文件保存到com.android.externalstorage.documents / tree /?

时间:2017-09-22 06:41:58

标签: android mediarecorder android-mediarecorder android-external-storage

我将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/

1 个答案:

答案 0 :(得分:0)

如果您创建一个名为DocumentFile的新file并使用ContentResolver openFileDescriptor(uri, "rwt")并得到一个ParcelableFileDescriptor,则将其命名为:pfd并调用pfd.getFileDescriptor()获得名为fd的Java FileDescriptor。然后,setOputputFile(fd)