简体   繁体   中英

Adding MP3 as Ringtone Android Write error

I am getting a write error when trying to add a sound as a ringtone in my app. I've tried different things but keep getting different errors. Not sure what to do.

The mp3s are passed into sound as URLS example:

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

The logcat shows:

.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

Edit:

I also have this in the manifest:

 <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" />

First about the Exception you got for security.

To Use WRITE_SETTINGS

First it should be present in the manifest file.

Second You should call Setting.System.canWrite() it return true or false based on whether you are allowed to write or not.

Third if the canWrite() returns false start ACTION_MANAGE_WRITE_SETTINGS . In this case user will allow the persmssions to write.

Now for Ringtone, You can save the Ringtone in your Phone Memory and you can then make it a ringtone. This is as follow:

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

and then you can call your funtion to set it as ringtone

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

        }

I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM