Android: set sound from SD card as ringtone

I am trying to set the ringtone from the sound that I saved on the SD card from my application. After going through every post that I could find on this, I find myself close, and simply do not define the right URI.

       File ringPath = new File(path, filename);

       ContentValues values = new ContentValues();
       values.put(MediaStore.MediaColumns.DATA, ringPath.getAbsolutePath());
       values.put(MediaStore.MediaColumns.TITLE, "temptitle");
       values.put(MediaStore.MediaColumns.SIZE, ringPath.length());
       values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
       values.put(MediaStore.Audio.Media.ARTIST, "tempartist");
       values.put(MediaStore.Audio.Media.IS_RINGTONE, true);

       uri = Uri.fromFile(ringPath);
       RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, uri);

Typically, a URI is defined as:

Uri uri = MediaStore.Audio.Media.getContentUriForPath(ringPath.getAbsolutePath());
Uri newUri = main.getContentResolver().insert(uri, values);

But I did not quite understand the essence of all this, and it gave me rather unpleasant errors (not that they were probably worse than my current ones). I am sure that this is a necessary way to do this, but the uri in my code actually returns the correct path (and the other does not, probably due to my misuse), so I do not understand why this will not work. Although, frankly, I see no reason to use a URI for this anyway, instead of specifying paths.

- -, , - , .

+3
2

, , path.so, , .


 File ringPath = new File(path, filename);

:


boolean b=ringPath.exists();

true, uri frm it.than, , ur.

0

, - > - > - > . . .

0

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


All Articles