在我的程序中,我想通过它的文件路径保存选定的铃声,然后将其设置为当前的铃声.
我从RingtonePreference获得了铃声uri,并从MediaStore数据库获取它的文件路径.
例如
Uri - content://media/internal/audio/media/29 Path - /system/media/audio/notifications/Ascend.mp3
现在,如何从我保存的文件路径中获取铃声Uri?
由于铃声已存在于MediaStore中,我尝试了以下功能,但它无法正常工作.
uriRingtone = MediaStore.Audio.Media.getContentUriForPath(szRingtonePath);
Uri与我从RingtonePreference得到的那个不一样.
uriRingtone - content://media/internal/audio/media
如何查询MediaStore以获得我需要的Uri?
ps我没有直接存储铃声Uri的原因是我发现同一铃声的Uri有时会在某些设备中发生变化.
小智 5
通过了解歌曲的标题,您可以恢复存储在RingtonePreference中的铃声URI的方式(据我所知).然后你可以通过使用游标来获取存储的铃声_id来查询它,你可以用它构建一个URI:
String ringtoneTitle = "<The desired ringtone title>";
Uri parcialUri = Uri.parse("content://media/external/audio/media"); // also can be "content://media/internal/audio/media", depends on your needs
Uri finalSuccessfulUri;
RingtoneManager rm = new RingtoneManager(getApplicationContext());
Cursor cursor = rm.getCursor();
cursor.moveToFirst();
while(!cursor.isAfterLast()) {
if(ringtoneTitle.compareToIgnoreCase(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.TITLE))) == 0) {
int ringtoneID = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
finalSuccessfulUri = Uri.withAppendedPath(parcialUri, "" + ringtoneID );
break;
}
cursor.moveToNext();
}
Run Code Online (Sandbox Code Playgroud)
其中finalSuccessful uri是uri指向RingtonePreference中的铃声.
您还可以对 MediaStore 中的任何内容以更通用的方式执行此操作。我必须从 URI 中获取路径并从路径中获取 URI。前者:
/**
* Gets the corresponding path to a file from the given content:// URI
* @param selectedVideoUri The content:// URI to find the file path from
* @param contentResolver The content resolver to use to perform the query.
* @return the file path as a string
*/
private String getFilePathFromContentUri(Uri selectedVideoUri,
ContentResolver contentResolver) {
String filePath;
String[] filePathColumn = {MediaColumns.DATA};
Cursor cursor = contentResolver.query(selectedVideoUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
cursor.close();
return filePath;
}
Run Code Online (Sandbox Code Playgroud)
后者(我为视频做的,但也可以用于音频或文件或其他类型的存储内容,通过将 MediaStore.Audio(等)替换为 MediaStore.Video:
/**
* Gets the MediaStore video ID of a given file on external storage
* @param filePath The path (on external storage) of the file to resolve the ID of
* @param contentResolver The content resolver to use to perform the query.
* @return the video ID as a long
*/
private long getVideoIdFromFilePath(String filePath,
ContentResolver contentResolver) {
long videoId;
Log.d(TAG,"Loading file " + filePath);
// This returns us content://media/external/videos/media (or something like that)
// I pass in "external" because that's the MediaStore's name for the external
// storage on my device (the other possibility is "internal")
Uri videosUri = MediaStore.Video.Media.getContentUri("external");
Log.d(TAG,"videosUri = " + videosUri.toString());
String[] projection = {MediaStore.Video.VideoColumns._ID};
// TODO This will break if we have no matching item in the MediaStore.
Cursor cursor = contentResolver.query(videosUri, projection, MediaStore.Video.VideoColumns.DATA + " LIKE ?", new String[] { filePath }, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(projection[0]);
videoId = cursor.getLong(columnIndex);
Log.d(TAG,"Video ID is " + videoId);
cursor.close();
return videoId;
}
Run Code Online (Sandbox Code Playgroud)
基本上,DATA列MediaStore(或您查询的任何子部分)存储文件路径,因此您可以使用该信息进行查找。
以下代码将返回音频,视频和图像的内容Uri的绝对路径.
public static String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = context.getContentResolver().query(contentUri, null, null, null, null);
int idx;
if(contentUri.getPath().startsWith("/external/image") || contentUri.getPath().startsWith("/internal/image")) {
idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
}
else if(contentUri.getPath().startsWith("/external/video") || contentUri.getPath().startsWith("/internal/video")) {
idx = cursor.getColumnIndex(MediaStore.Video.VideoColumns.DATA);
}
else if(contentUri.getPath().startsWith("/external/audio") || contentUri.getPath().startsWith("/internal/audio")) {
idx = cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DATA);
}
else{
return contentUri.getPath();
}
if(cursor != null && cursor.moveToFirst()) {
return cursor.getString(idx);
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22575 次 |
| 最近记录: |