Android 从音频Uri获取真实路径
在我的应用程序中,用户可以使用RingtonePreference选择通知。从后者中,我能够检索所选通知的Uri,并使用以下代码提取真实文件名:Android 从音频Uri获取真实路径,android,Android,在我的应用程序中,用户可以使用RingtonePreference选择通知。从后者中,我能够检索所选通知的Uri,并使用以下代码提取真实文件名: private String getUriRealPath(Uri contentUri) { if (BuildConfig.DEBUG) { Log.d(TAG, "Getting real path of uri: " + contentUri.toString()); } String path = n
private String getUriRealPath(Uri contentUri) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "Getting real path of uri: " + contentUri.toString());
}
String path = null;
final String[] projection = new String [] { MediaStore.Audio.Media.DATA };
final Cursor cursor;
try {
cursor = mContext.getContentResolver().query(contentUri, projection, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
int idx = cursor.getColumnIndex(projection[0]);
if (idx != -1) {
path = cursor.getString(idx);
if (BuildConfig.DEBUG) {
Log.d(TAG, "Real path is: " + path);
}
}
else {
if (BuildConfig.DEBUG) {
Log.d(TAG, "Path can't be resolved.");
}
}
}
} catch (Exception e) {
Log.e(TAG, "getUriRealPath - " + e);
}
return path;
}
但是,一旦用户选择了通过第三方下载的通知,上述代码就无法找到真正的路径。
提取的原因是我需要在声音池对象中播放通知的路径
我可能看得太多了,但是getContentResolver()为我的应用程序返回一个ContentResolver实例。我应该使用“全局”内容解析器吗
但是,一旦用户选择了通过第三方下载的通知,上述代码就无法找到真正的路径
无论如何,您可能无法访问实际文件。首先,它可能不是以文件的形式存在,而是以流的形式存在。其次,它可能不在您具有读取权限的存储器中。您只能通过提供给您的Uri
可靠地访问此媒体
提取的原因是我需要在声音池对象中播放通知的路径
然后,您必须停止使用声音池
,切换到MediaPlayer
、AudioTrack
,或其他功能
我应该使用“全局”内容解析器吗
没有“全局”
ContentResolver
该死。我喜欢SoundPool。@wilenx:老实说,在你提到它之前,我不知道它有这个限制——我认为它可以与Uri
一起工作,就像MediaPlayer
一样。