Getting file from content uri for videos taken

余生长醉 提交于 2019-12-10 17:39:56

问题


I have a method below:

private String getRealPathFromUriForVideos(Uri selectedVideoUri) {
    String wholeID = DocumentsContract.getDocumentId(selectedVideoUri);
    String id = wholeID.split(":")[1];

    String[] column = { MediaStore.Video.Media.DATA };
    String sel = MediaStore.Video.Media._ID + "=?";
    Cursor cursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{ id }, null);
    String filePath = "";

    int columnIndex = cursor.getColumnIndex(column[0]);
    if (cursor.moveToFirst()) {
        filePath = cursor.getString(columnIndex);
    }
    cursor.close();
    return filePath;
}

This works just fine getting the file for videos that hte user selects. However, I want to allow users to also create new videos (from my app) and then get the URI and the file from there. The URI for newly created videos is: content://media/external/video/media/41. For selected videos is like content://com.android.providers.media.documents/document/video%3A42.

It works with the second one but not the first one. First one I get IllegalArgumentException because its not a document URI. How can I get the file from the first URI?


回答1:


This works just fine getting the file for videos that hte user selects

It may work in a few situations. It will not work in general. A Uri that you get from something like ACTION_OPEN_DOCUMENT does not have to represent a file, let alone one that you can access via the filesystem, let alone one that this script-kiddie algorithm will let you access.

The URI for newly created videos is: content://media/external/video/media/41

Not necessarily. I suppose that there is a way that you get a Uri like that for a recorded video, though off the top of my head I cannot think of a recommended way that would give you such a Uri. If you are using MediaRecorder or ACTION_VIDEO_CAPTURE, you create your own file (and, for ACTION_VIDEO_CAPTURE, your own Uri for that file). And, if you are creating your own file, you know where that file is.

Need to be able to upload to my server

For the video, record to a file that you control, then use that file.

Use some library that lets you upload from a Uri or InputStream. Otherwise:

  • Use ContentResolver and openFileInput() to get an InputStream on the content represented by the Uri
  • Create a FileOutputStream on some file that you control (e.g., in getCacheDir())
  • Copy the content from the InputStream to the OutputStream
  • Use your copy for the upload
  • Delete your copy when the work is done

You treat a foreign Uri as if it were a URL to a Web server: stream the content.




回答2:


Seems to get it from the second URI I need this:

private String getRealPathFromUriForImagesAndVideo(Uri contentUri) {
    Cursor cursor = null;
    try {
        String[] proj = {MediaStore.Images.Media.DATA};
        cursor = getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } catch (Exception e) {
        return contentUri.getPath();
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}


来源:https://stackoverflow.com/questions/40348407/getting-file-from-content-uri-for-videos-taken

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!