Android:从内容URI获取文件URI? [英] Android: Getting a file URI from a content URI?

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

问题描述

在我的应用程序中,用户是选择应用程序处理的音频文件。问题是,为了使应用程序做我想要的音频文件,我需要的URI是文件格式。当我使用Android的本地音乐播放器浏览应用程序中的音频文件时,URI是一个内容URI,如下所示:

In my app the user is to select an audio file which the app then handles. The problem is that in order for the app to do what I want it to do with the audio files, I need the URI to be in file format. When I use Android's native music player to browse for the audio file in the app, the URI is a content URI, which looks like this:

content://media/external/audio/media/710

然而,使用流行文件管理器应用程序Astro,我得到以下内容:

However, using the popular file manager application Astro, I get the following:

file:///sdcard/media/audio/ringtones/GetupGetOut.mp3

后者对我来说更容易使用,但当然我希望该应用有用户选择的音频文件的功能,无论他们用来浏览其收藏的程序如何。所以我的问题是,有没有办法将内容://风格的URI转换成一个文件:// URI?否则,你会建议我如何解决这个问题?这是调用选择器的代码,供参考:

The latter is much more accessible for me to work with, but of course I want the app to have functionality with the audio file the user chooses regardless of the program they use to browse their collection. So my question is, is there a way to convert the content:// style URI into a file:// URI? Otherwise, what would you recommend for me to solve this problem? Here is the code which calls up the chooser, for reference:

Intent ringIntent = new Intent();
ringIntent.setType("audio/mp3");
ringIntent.setAction(Intent.ACTION_GET_CONTENT);
ringIntent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(ringIntent, "Select Ringtone"), SELECT_RINGTONE);

非常感谢!请让我知道,如果我可以提供更多的代码来帮助回答。

Thank you very much! Please let me know if I can provide more code to help with answering.

我在内容URI中执行以下操作:

I do the following with the content URI:

m_ringerPath = m_ringtoneUri.getPath();
File file = new File(m_ringerPath);

然后用所述文件做一些FileInputStream的东西。

Then do some FileInputStream stuff with said file.

推荐答案

只需使用 getContentResolver()。openInputStream(uri)获取 InputStream 从一个URI。

Just use getContentResolver().openInputStream(uri) to get an InputStream from a URI.

http://developer.android.com/reference/android/content/ContentResolver.html#openInputStream(android.net.Uri)

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

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