【问题标题】:convert url from sdcard to content uri将 url 从 sdcard 转换为内容 uri
【发布时间】:2013-05-08 12:02:09
【问题描述】:

我想绕过 Intent.ACTION_PICK 这样做我需要插入内容 uri,但我有带 url 的字符串。

我需要转换这种格式:

 /mnt/sdcard/Movies/Your_voice/Your_voice080513_141510.mp4

转 uri 格式:

content://media/external/video/media/2308

我发现了这个:

How to convert a file:// uri into content:// uri?

还有这个图像 Convert file uri to content uri

更新:

我有一个发送文件的方法,这个方法获取内容 uri。

所以要使用这种方法,我正在使用意图,因为输出是内容 uri。

我正在使用这个意图:

Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("video/*");
        startActivityForResult(intent, RESULT_PICK_IMAGE_CROP);

此意图打开文件夹,用户选择文件夹和之后的视频文件

我之前的问题: intent ACTION_PICK in specific folder

问题是我只需要特定的文件夹,但我这里是红色的

Using Intent.ACTION_PICK for specific path

这是不可能的。

所以我尝试将我必须的路径转换为内容 url

【问题讨论】:

标签: android uri


【解决方案1】:

片段:

Uri.fromFile(new File("/mnt/sdcard/Movies/Your_voice/Your_voice080513_141510.mp4"))

Uri.parse(new File("/mnt/sdcard/Movies/Your_voice/Your_voice080513_141510.mp4").toString())

【讨论】:

  • 我收到文件:///mnt/sdcard/Movies/Your_voice/Your_voice080513_141510.mp4 不是这种格式的内容://media/external/video/media/2308
【解决方案2】:

你可以使用

Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File("%PATH_TO_FILE%/test.mp4");
intent.setDataAndType(Uri.fromFile(file), "video/*");
startActivity(intent);

这将绕过选择使用默认视频播放器打开文件

您可以从这样的文件中获取内容 URI

Uri myUri = Uri.fromFile(new File("/sdcard/cats.jpg"));

或者像这样

Uri myUri = Uri.parse(new File("/sdcard/cats.jpg").toString());

这应该给你一个你可以使用的 Uri

参考:Get content uri from file path in android

【讨论】:

  • 我试试这个,但我不知道为什么不工作 Intent intent = new Intent();意图.setAction(android.content.Intent.ACTION_VIEW); File file = new File("/mnt/sdcard/Movies/Your_voice/Your_voice080513_141510.mp4"); intent.setDataAndType(Uri.fromFile(file), "video/*"); //开始活动(意图); startActivityForResult(intent, RESULT_PICK_IMAGE_CROP);
  • 什么是 RESULT_PICK_IMAGE_CROP?那是你给自己设置的面具吗?没听说过
  • 它只是最终的 int 像 4 ,结果我有开关。在结果中 FileURI = data.getData();
  • 您确定您尝试启动的应用程序会返回结果吗?你得到什么错误。如果没有像 Logcats 这样的东西来查看实际发生的事情,很难提供帮助。这可能是很多事情。是否肯定有兼容 MP4 的视频播放器。视频文件肯定存在吗?另外,你想对这个名为 RESULT_PICK_IMAGE_CROP 标志的视频做什么
  • 更新了我关于如何从文件中获取内容 Uri 的答案
猜你喜欢
  • 2011-11-10
  • 2014-08-13
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 2013-07-31
  • 1970-01-01
相关资源
最近更新 更多