从内容 uri 获取所拍摄视频的文件 [英] Getting file from content uri for videos taken

查看:26
本文介绍了从内容 uri 获取所拍摄视频的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下方法:

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;
}

这可以很好地获取用户选择的视频的文件.但是,我希望允许用户也创建新视频(从我的应用程序),然后从那里获取 URI 和文件.新创建的视频的 URI 是:content://media/external/video/media/41.对于选定的视频,类似于 content://com.android.providers.media.documents/document/video%3A42.

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.

它适用于第二个,但不适用于第一个.第一个我得到 IllegalArgumentException 因为它不是文档 URI.如何从第一个 URI 获取文件?

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?

推荐答案

这可以很好地获取用户选择的视频的文件

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

它可能在某些情况下起作用.它不会在一般情况下工作.您从 ACTION_OPEN_DOCUMENT 之类的东西中获得的 Uri 不必代表文件,更不用说可以通过文件系统访问的文件了,更不用说这个脚本小子的文件了算法会让你访问.

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.

新建视频的 URI 为:content://media/external/video/media/41

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

不一定.我想有一种方法可以让你获得一个像录制视频那样的 Uri ,尽管在我的脑海里我想不出一种推荐的方法来给你这样的 Uri.如果您使用 MediaRecorderACTION_VIDEO_CAPTURE,您可以创建自己的文件(对于 ACTION_VIDEO_CAPTURE,您自己的 Uri对于那个文件).而且,如果您要创建自己的文件,您就知道该文件在哪里.

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.

使用一些可以让您从 UriInputStream 上传的库.否则:

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

  • 使用ContentResolveropenFileInput()Uri表示的内容上获取InputStream立>
  • 在您控制的某个文件上创建一个 FileOutputStream(例如,在 getCacheDir() 中)
  • InputStream中的内容复制到OutputStream
  • 使用您的副本上传
  • 完成工作后删除您的副本
  • 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

您将外部 Uri 视为 Web 服务器的 URL:流式传输内容.

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

这篇关于从内容 uri 获取所拍摄视频的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆