私のアプリでは、ユーザーはRingtonePreferenceを使用して通知を選択できます。後者から、選択した通知のURIを取得し、次のコードを使用して実際のファイル名を抽出できます。
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;
}
ただし、ユーザーがサードパーティ経由でダウンロードされた通知を選択すると、上記のコードは実際のパスを見つけることができません。抽出の理由は、SoundPoolオブジェクトで通知を再生するためのパスが必要なためです。
私はこれを見過ぎているかもしれませんが、getContentResolver()は私のアプリケーションのContentResolverインスタンスを返します。「グローバル」ContentResolverを使用する必要がありますか?