将MP3添加为铃声Android写入错误

时间:2017-08-24 15:10:38

标签: java android android-studio android-permissions

尝试在我的应用中添加声音作为铃声时出现写入错误。 我尝试过不同的东西,但不断出现不同的错误。不知道该怎么办。

将mp3作为URL示例传递给声音:

sound = {"http://www.sound.com/file.mp3"}



 public void setRingToneInApp() {

        File k = new File(sound[+position]); // path is a file to /sdcard/media/ringtone
        ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
        values.put(MediaStore.MediaColumns.TITLE, "My Song title");
        values.put(MediaStore.MediaColumns.SIZE, 215454);
        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
        values.put(MediaStore.Audio.Media.ARTIST, "Annoying Sound");
        values.put(MediaStore.Audio.Media.DURATION, 230);
        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);
        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);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Log.i("", "Build.VERSION.SDK_INT=" + Build.VERSION.SDK_INT);
            if (Settings.System.canWrite(context)) {
                Log.i("", "Settings.System.canWrite=true");
                RingtoneManager.setActualDefaultRingtoneUri(context.getApplicationContext(), RingtoneManager.TYPE_RINGTONE, newUri);
            } else {
                Log.i("", "try to get write permissin from user");
                RingtoneManager.setActualDefaultRingtoneUri(context.getApplicationContext(), RingtoneManager.TYPE_RINGTONE, newUri);
            }
        }
    }

logcat显示:

.893 20546-20546/oidRuntime: FATAL EXCEPTION: main
                                                                             Process: 20546
                                                                             java.lang.SecurityException: t granted  this permission: android.permission.WRITE_SETTINGS.
                                                                                 at android.os.Parcel.readException(Parcel.java:1683)
                                                                                 at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
                                                                                 at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
                                                                                 at android.content.ContentProviderProxy.call(ContentProviderNative.java:646)
                                                                                 at android.provider.Settings$NameValueCache.putStringForUser(Settings.java:1567)
                                                                                 at android.provider.Settings$System.putStringForUser(Settings.java:1938)
                                                                                 at android.media.RingtoneManager.setActualDefaultRingtoneUri(RingtoneManager.java:666)
                                                                                 at ListAdapter.setRingToneInApp(CustomListAdapter.java:257)
                                                                                 at ListAdapter$3.onClick(CustomListAdapter.java:101)
                                                                                 at android.view.View.performClick(View.java:5610)
                                                                                 at android.view.View$PerformClick.run(View.java:22265)
                                                                                 at android.os.Handler.handleCallback(Handler.java:751)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:154)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
08-24 15:02:57.893 1583-1751/? W/ActivityManager:   Force finishing activity ctivity
08-24 15:02:57.896 1583-1751/? W/ActivityManager:   Force finishing activity ctivity
08-24 15:02:57.896 1583-1751/? W/ActivityManager: Duplicate finish request for ActivityRecord{22c90f8 u0 ctivity t132 f}
08-24 15:02:57.943 1583-14442/? I/OpenGLRenderer: Initialized EGL, version 1.4
08-24 15:02:57.943 1583-14442/? D/OpenGLRenderer: Swap behavior 1
08-24 15:02:57.943 1583-14442/? W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
08-24 15:02:57.943 1583-14442/? D/OpenGLRenderer: Swap behavior 0
08-24 15:02:57.960 1583-14442/? D/EGL_emulation: eglCreateContext: 0x9cb7fee0: maj 2 min 0 rcv 2
08-24 15:02:58.003 1583-14442/? D/EGL_emulation: eglMakeCurrent: 0x9cb7fee0: ver 2 0 (tinfo 0x8bbe9730)
08-24 15:02:58.015 1583-14442/? D/EGL_emulation: eglMakeCurrent: 0x9cb7fee0: ver 2 0 (tinfo 0x8bbe9730)
08-24 15:02:58.396 1583-1596/? W/ActivityManager: Activity pause timeout for ActivityRecord{22c90f8 u0 ctivity t132 f}
08-24 15:02:58.406 1303-1340/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 2650112
08-24 15:02:58.431 1303-1303/? E/EGL_emulation: tid 1303: eglCreateSyncKHR(1901): error 0x3004 (EGL_BAD_ATTRIBUTE)
08-24 15:02:58.613 16409-20813/? D/Index: Indexing locale 'en_US' took 24 millis

编辑:

我在清单中也有这个:

 <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission-sdk-23 android:name="android.permission.WRITE_SETTINGS" />

1 个答案:

答案 0 :(得分:0)

首先是关于安全性的例外情况。

使用Setting.System.canWrite()

首先它应该出现在清单文件中。

第二你应该根据你是否被允许写作canWrite()来回复真或假。

如果false返回InputStream input = new BufferedInputStream(url.openStream()); File sdCard = Environment.getExternalStorageDirectory(); File dir = new File (sdCard.getAbsolutePath() + "/myfoldername/dir3"); dir.mkdirs(); File file = new File(dir, myringtonename + ".mp3"); FileOutputStream output = new FileOutputStream(file); setringtone(file); 开始AWS documentation on creating custom AMIs for Elastic Beanstalk,则为第三。在这种情况下,用户将允许写入。

现在,对于铃声,您可以将铃声保存在手机记忆库中,然后可以将其设为铃声。具体如下:

private void setringtone(File file) {
            ContentValues values = new ContentValues();
            values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
            values.put(MediaStore.MediaColumns.TITLE, "My Song title");
            values.put(MediaStore.MediaColumns.SIZE, 215454);
            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
            values.put(MediaStore.Audio.Media.ARTIST, "You");
            values.put(MediaStore.Audio.Media.DURATION, 230);
            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);
            Uri uri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath());
            Uri newUri = getContentResolver().insert(uri, values);
 RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(),RingtoneManager.TYPE_RINGTONE,newUri);

        }

然后你可以调用你的功能将其设置为铃声

#cup { z-index: -1; }
/* or */
#registration_form { position: relative; }

我希望它有所帮助。