How to get Uri from MediaStore via file path?

In my program, I want to save the selected ringtone by its path to the file, and then set it as the current ringtone later.

I have a uri ringtone from RingtonePreference and get the path to it from the MediaStore database.

eg.

  Uri - content: // media / internal / audio / media / 29
 Path - /system/media/audio/notifications/Ascend.mp3

Now, how do I get the Uri ringtone from the file path that I saved?

Since the ringtone already exists in MediaStore, I tried the following functions, but it does not work.

  uriRingtone = MediaStore.Audio.Media.getContentUriForPath (szRingtonePath);

Uri is not the same as what I got from RingtonePreference.

  uriRingtone - content: // media / internal / audio / media

How do I request MediaStore to get the Uri I need?

ps the reason why I don’t store the Uri ringtone directly is because I found that the Uri for the same ringtone can sometimes change on some device.

+6
source share
4 answers

How can you restore the ringtone URIs stored in RingtonePreference (as far as I know), knowing the name of the song. Then you can query it with the cursor to get the _id ringtone, and with it you can create a 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(); } 

where finalSuccessful uri is the uri pointing to the ringtone in RingtonePreference.

+5
source

You can also do this in a more universal way for any content in MediaStore. I have to get the path from the URI and get the URI from the paths. First:

 /** * 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; } 

The latter (which I do for video, but can also be used for audio or files or other types of stored content, replacing MediaStore.Audio (etc.) for 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 the MediaStore 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; } 

Basically, the DATA MediaStore column (or any subsector you request) saves the file path, so you use this information to search.

+3
source

The following code will return the absolute path for the Uri content of audio, video, and image.

 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; } 
+2
source

@ dong221: Use internal URI as MediaStore.Audio.Media.INTERNAL_CONTENT_URI.

+1
source

Source: https://habr.com/ru/post/887455/


All Articles