I already know this question has been asked many times and answered, But this is something different. I am trying to set a Tone in android-11 and tried all the possible solutions over the internet and SO.
The most helpful question and answer which I found is How to set a file as a ringtone for Android 10? but still, I couldn't make it happen.
The thing is I am able to save my raw folder.mp3 file to local storage in my phone but when I try to set this as a tone I am getting exceptions and I am unable to set it. I have tried putting log and all only this below 2 lines not getting executed. If anyone can help that would be much appreciated. Thank you.
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
Uri newUri = getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(MainActivity.this, RingtoneManager.TYPE_RINGTONE, newUri);
I have already given storage permission and also runtime checked about all the permission. Also i have set android:requestLegacyExternalStorage="true"
my whole code
InputStream fIn = getBaseContext().getResources().openRawResource(R.raw.ring);
try {
byte[] buffer = new byte[fIn.available()];
fIn.read(buffer);
fIn.close();
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES) + "/Tone/";
String filename = "Ring.mp3";
if (!new File(path).exists()) {
new File(path).mkdirs();
}
try {
FileOutputStream save = new FileOutputStream(path + filename);
save.write(buffer);
save.flush();
save.close();
sendBroadcast(new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE", Uri.parse("file://" + path + filename)));
File k = new File(path, filename);
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "RingTone");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.MediaColumns.ARTIST, R.string.app_name);
values.put(MediaStore.Audio.Media.IS_RINGTONE, Boolean.valueOf(true));
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, Boolean.valueOf(false));
values.put(MediaStore.Audio.Media.IS_ALARM, Boolean.valueOf(false));
values.put(MediaStore.Audio.Media.IS_MUSIC, Boolean.valueOf(false));
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
Uri newUri = getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(MainActivity.this, RingtoneManager.TYPE_RINGTONE, newUri);
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e2) {
e2.printStackTrace();
return false;
}
} catch (IOException e3) {
e3.printStackTrace();
return false;
}
Also i have tried with another method but getting same result like unable to set as ringtone
Uri newUri = getContentResolver().insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
try (OutputStream os = getContentResolver().openOutputStream(newUri)) {
//copy your file from asset into os here
int size = (int) f.length();
byte[] bytes = new byte[size];
try {
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(f));
buf.read(bytes, 0, bytes.length);
buf.close();
os.write(bytes);
os.close();
os.flush();
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception ignored) {
ignored.printStackTrace();
}
}