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

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

问题描述

在我的应用程序中,用户将选择应用程序处理的音频文件.问题是,为了让应用程序执行我希望它对音频文件执行的操作,我需要 URI 为文件格式.当我使用Android的原生音乐播放器在app中浏览音频文件时,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

后者对我来说更容易使用,但当然我希望应用程序具有用户选择的音频文件的功能,而不管他们使用什么程序浏览他们的收藏.所以我的问题是,有没有办法将 content:// 样式 URI 转换为 file:// 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);

我对内容 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) 从 URI 中获取 InputStream.

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天全站免登陆