Get filename and path from URI from mediastore
Below API 19 use this code to get File Path from URI:
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
Android - Get filename and path of URI from mediastore
exif constructor wants the file's path as String not as URI.
Use android.support.media.ExifInterface
. It has a constructor that takes an InputStream
. You get the InputStream
by calling getContentResolver().openInputStream(uri)
on some Context
(e.g., your Activity
), where uri
is your Uri
.
So why all of that doesn't work?
Because that code makes a lot of unfounded assumptions about the Uri
. There is no requirement for a Uri
to refer to a file on the filesystem that you can access, and there is no requirement that your query()
supports a DATA
column.
Get filename and path from URI from mediastore in Nought and Oreo device
Use a ContentResolver
and openInputStream()
to get an InputStream
on the content identified by the Uri
. Ideally, just use that stream directly, for whatever it is that you are trying to do. Or, use that InputStream
and some FileOutputStream
on a file that you control to make a copy of the content, then use that file.
This is working properly in devices before Android N
How to get the Full file path from URI
Use:
String path = yourAndroidURI.uri.getPath() // "/mnt/sdcard/FileName.mp3"
File file = new File(new URI(path));
or
String path = yourAndroidURI.uri.toString() // "file:///mnt/sdcard/FileName.mp3"
File file = new File(new URI(path));
How to get the Uri from MediaStore via file path?
The way you can recover the ringtone URI stored in the RingtonePreference is (as far as I know) by knowing the title of the song. Then you can query it by using a cursor to obtain the ringtone _id stored and with it you can build an 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 the RingtonePreference.
get path of uri from a cursor on MediaStore
A cursor is in effect a dataset that was returned. Imagine it like a spreadsheet with rows(each track) and columns. Columns contain the separate pieces of data, a row represents one record.
each column is accessed by getting it's index ie position to the right in the spreadsheet table
private final String track_id = MediaStore.Audio.Media._ID;
private final String bucket_displayname = MediaStore.Audio.Media.BUCKET_DISPLAY_NAME;
private final String track_no = MediaStore.Audio.Media.TRACK;
private final String track_name = MediaStore.Audio.Media.TITLE;
private final String artist = MediaStore.Audio.Media.ARTIST;
private final String artist_id = MediaStore.Audio.Media.ARTIST_ID;
private final String duration = MediaStore.Audio.Media.DURATION;
private final String album = MediaStore.Audio.Media.ALBUM;
private final String composer = MediaStore.Audio.Media.COMPOSER;
private final String year = MediaStore.Audio.Media.YEAR;
private final String path = MediaStore.Audio.Media.DATA;
private final String date_added = MediaStore.Audio.Media.DATE_ADDED;
private final String genre = MediaStore.Audio.Media.GENRE;
private final String genre_id = MediaStore.Audio.Media.GENRE_ID;
and so on. Note there is a column called MediaStore.Audio.Media.DATA (apart from android10 when google decided to do away with this only to bring it back in 11)
So when you loop around your cursor, you effectively read each row of your spreadsheet.
Related Topics
How to Show Current Location on a Google Map on Android Marshmallow
How to Filter Specific Apps For Action_Send Intent (And Set a Different Text For Each App)
What Is the List of Supported Languages/Locales on Android
What Is Android:Weightsum in Android, and How Does It Work
How to Display a Toast from a Background Thread on Android
Android and Xmpp: Currently Available Solutions
"Debug Certificate Expired" Error in Eclipse Android Plugins
Playing Html5 Video on Fullscreen in Android Webview
How to Update a Single Row in a Listview
How to Set the Part of the Text View Is Clickable
Getting the Screen Density Programmatically in Android
How to Add Dividers and Spaces Between Items in Recyclerview
Android Studio Adds Unwanted Permission After Running Application on Real Device
How to Build a Horizontal Listview With Recyclerview
Android Fatal Signal 11 (Sigsegv) At 0X636F7D89 (Code=1). How Can It Be Tracked Down