I want to set ringtone when download mp3 from url and click the setRingtoneButton. I think I did most of the work but there are some missing. If I download mp3 and click set ringtone button, device sound changed but it will be "silent" instead of my selected mp3.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.System.canWrite(context.getApplicationContext())) {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + context.getPackageName()));
((Activity) context).startActivityForResult(intent, 200);
} else {
File k = new File(Environment.getExternalStorageDirectory() + File.separator + "AppName" + File.separator + item.getRing().getName() + ".mp3"); // path is a file to /sdcard/media/ringtone
if (k.exists()) {
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.Audio.Media.ARTIST, "Artist");
values.put(MediaStore.MediaColumns.SIZE, 330);
values.put(MediaStore.MediaColumns.TITLE, item.getRing().getName());
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.DURATION, item.getRing().getDuration());
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
//Insert it into the database
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
//context.getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" +k.getAbsolutePath() + "\"", null);
Uri newUri = context.getContentResolver().insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values);
RingtoneManager ringtoneManager = new RingtoneManager(context);
try {
Uri rUri = RingtoneManager.getValidRingtoneUri(context);
if (rUri != null)
ringtoneManager.setStopPreviousRingtone(true);
RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(),
RingtoneManager.TYPE_RINGTONE,
newUri);
Toast.makeText(context, "set ringtone", Toast.LENGTH_SHORT).show();
} catch (Throwable t) {
Log.e(TAG, "catch exception");
}
Toast.makeText(context, context.getString(R.string.adapter_ringtonesuccessmsg), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, context.getString(R.string.adapter_errordownloadmessage), Toast.LENGTH_SHORT).show();
}
}
}
I cant fix that despite all my efforts.
I change these columns;
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
Uri newUri = context.getContentResolver().insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values);
to
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
context.getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
Uri newUri = context.getContentResolver().insert(uri, values);
And this way I can set ringtone from URL.