来自 url 的 setRingtone 和适配器类中的 RecyclerView (Android)

塞尔曼·托森

我想在从 url 下载 mp3 时设置铃声并单击 setRingtoneButton。我想我做了大部分工作,但还有一些缺失。如果我下载 mp3 并单击设置铃声按钮,设备声音会改变,但它将是“静音”而不是我选择的 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 设置铃声。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

什么是Android设备上查看的数据绑定在RecyclerView适配器类的正确方法?

使用Android Studio中的Recyclerview适配器从Firebase中获取特定密钥

从适配器中刷新整个RecyclerView

适配器中的Android RecyclerView显示问题

E / RecyclerView:未连接适配器;跳过Android,kotlin中的布局

Android | 在ViewHolder onClick上更新RecyclerView适配器

如何从Android中的RecyclerView适配器获取物品

Android:RecyclerView Click Listener未进入适配器

RecyclerView适配器中的数据绑定-Android

android recyclerview适配器进度条不显示

如何从适配器到Android Kotlin recyclerview中的活动获取变量值?

非活动类或RecyclerView适配器类中的SharedPreference

Android RecyclerView.Adapter允许在单个适配器中列出不同对象类型吗?

到recyclerview的Firebase数据显示错误-RecyclerView:android中未连接适配器

RecyclerView适配器中的onBackPressed()

如何从Android Kotlin中的RecyclerView适配器获取arraylist

Android中的ListView和适配器,是否属于适配器设计模式?

来自URL的图像未加载到自定义适配器中

Android-在Listview中显示来自适配器的数据

在RecyclerView适配器中显示FragmentDialog

Android-Recyclerview适配器显示来自外部存储的图像

Android RecyclerView适配器位置为-1

Android从活动更改RecyclerView适配器上的TextView textSize

RecyclerView() 适配器

RecyclerView 适配器 Android Studio

如何在android java中的数据绑定recyclerview适配器中创建2列

库模块中的 Android RecyclerView 适配器

带有 RecyclerView 适配器的 Android ViewModel

android - 在 RecyclerView 适配器中创建和使用 ViewBinder