How to get the Uri from MediaStore via file path?

后端 未结 4 954
别那么骄傲
别那么骄傲 2020-12-15 12:53

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

I have got the ringtone uri from RingtonePrefere

4条回答
  •  有刺的猬
    2020-12-15 13:07

    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 = "";
    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.

提交回复
热议问题