从乌里从视频选配获取文件路径 [英] Get file path from Uri from Video Chooser

查看:175
本文介绍了从乌里从视频选配获取文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我开始一个Intent挑这样的视频文件

In my app I start an Intent to pick a video file like this

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("video/*");
startActivityForResult(intent, kVideoPickerRequestCode);

当它回来我得到URI是这样的:

When it comes back i get the Uri like this:

if(resultCode == RESULT_OK) {
  if (requestCode == kVideoPickerRequestCode) {
    Uri selectedVideoURI = data.getData();
  }
}

这乌里似乎是罚款,我VideoView。它能够播放拾取视频完美。但现在我想将视频上传到服务器,因此需要一个文件。

This Uri seems to be fine for my VideoView. It plays back the picked video perfectly. But now I'd like to upload the video to a server and therefore need a File.

不管我如何努力的URI转换为String路径,失败......我看了一些帖子SO这个样
<一href=\"http://stackoverflow.com/questions/7154292/how-to-convert-content-uri-into-actual-file-path\">How内容转换://乌里转化为实际的文件路径?
要么
<一href=\"http://stackoverflow.com/questions/17558560/filenotfoundexception-when-trying-to-upload-a-recorded-video-to-server-from-andr\">FileNotFoundException尝试将录制的视频从Android应用上传到服务器时,
但它只是不为我工作。

No matter how i try to convert the Uri to a String-Path, it fails... I read some SO posts on this like How to convert content:// Uri into actual file path? or FileNotFoundException when trying to upload a recorded video to server from Android app but it's just not working for me.

code这样始终返回NULL作为路径:

Code like this always returns NULL as the path:

public static String getRealPathFromUri(Activity activity, Uri contentUri) {
  String[] proj = { MediaStore.Images.Media.DATA };
  Cursor cursor = getActivity().getContentResolver().query(uri, proj,  null, null, null);
  int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  cursor.moveToFirst();
  return cursor.getString(column_index);
}

我的测试设备运行的是Android 4.4.2。它可以是它的一些安全性问题奇巧?

My test-device is running Android 4.4.2. Can it be that it's some security issue with KitKat?

感谢您的任何帮助提示!

Thanks for any helping hint!

推荐答案

您乌里不一定再present磁盘上的文件。它开始与内容://这意味着它是由内容提供者为该类型的文件提供了

Your Uri does not necessarily represent a file on the disk. It begins with content:// meaning it is served up by the content provider for that type of file.

您实际上并不需要在这里使用文件系统中的文件(尽管可以)。你想要做什么是数据流从内容提供者直接向服务器

You don't actually need to use a file in the filesystem here (although, you can). What you want to do is stream the data from the content provider directly to the server.

有一些很好的例子 - 我会尝试这个对于如何做到这一点,在第一部分如何访问现有的内容提供商的二进制数据

There are some good examples - I would try this one for how to do this, in the first section "How to access binary data of existing content providers"

这篇关于从乌里从视频选配获取文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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