使用适配器 class 中的 RecyclerView 从 url 设置铃声 (Android)

setRingtone from url with RecyclerView in adapter class (Android)

我想在从 url 下载 mp3 时设置铃声,然后单击设置铃声按钮。我想我做了大部分工作,但还有一些遗漏。如果我下载 mp3 并单击设置铃声按钮,设备声音会改变,但它将是 "silent" 而不是我选择的 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();
                                    }
                                }
                            }

尽管我尽了一切努力,我还是无法解决这个问题。

我更改了这些列;

Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());

Uri newUri = context.getContentResolver().insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values);

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);

这样我就可以设置 URL 的铃声。