2

I am trying to set an audio file in the /res/raw/ folder as the default ringtone programmatically. I have the following file like so: myapp/res/raw/file.mp3.

I have the following code, but it unfortunately completely fails to set the ringtone. I'm not sure what is causing the issue, but was wondering if anyone had any insight to share.

Uri path = Uri.parse("android.resource://com.mikan.myapp/res/raw/file.mp3");
        RingtoneManager.setActualDefaultRingtoneUri(
                getApplicationContext(), RingtoneManager.TYPE_RINGTONE,
                path);
        Log.i("TESTT", "Ringtone Set to Resource: "+ path.toString());
        RingtoneManager.getRingtone(getApplicationContext(), path)
                .play();

I'm getting the following exception (which is caught within RingtoneManager, making it look like nothing is happening at all:

03-25 20:20:23.024: D/MediaPlayer(24659): setDataSource IOException happend : 
03-25 20:20:23.024: D/MediaPlayer(24659): java.io.FileNotFoundException: More than two path segments: android.resource://com.mikan.myapp/res/raw/file.mp3
03-25 20:20:23.024: D/MediaPlayer(24659):   at android.content.ContentResolver.getResourceId(ContentResolver.java:861)
03-25 20:20:23.024: D/MediaPlayer(24659):   at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:653)
03-25 20:20:23.024: D/MediaPlayer(24659):   at android.media.MediaPlayer.setDataSource(MediaPlayer.java:979)
03-25 20:20:23.024: D/MediaPlayer(24659):   at android.media.MediaPlayer.setDataSource(MediaPlayer.java:933)
03-25 20:20:23.024: D/MediaPlayer(24659):   at android.media.Ringtone.setUri(Ringtone.java:228)
03-25 20:20:23.024: D/MediaPlayer(24659):   at android.media.RingtoneManager.getRingtone(RingtoneManager.java:647)
03-25 20:20:23.024: D/MediaPlayer(24659):   at android.media.RingtoneManager.getRingtone(RingtoneManager.java:628)
03-25 20:20:23.024: D/MediaPlayer(24659):   at com.mikan.myapp.CustomApp.saveas(CustomApp.java:139)
03-25 20:20:23.024: D/MediaPlayer(24659):   at com.mikan.myapp.ListAdapter$1.onClick(ListAdapter.java:83)
03-25 20:20:23.024: D/MediaPlayer(24659):   at android.view.View.performClick(View.java:4475)
03-25 20:20:23.024: D/MediaPlayer(24659):   at android.view.View$PerformClick.run(View.java:18786)
03-25 20:20:23.024: D/MediaPlayer(24659):   at android.os.Handler.handleCallback(Handler.java:730)
03-25 20:20:23.024: D/MediaPlayer(24659):   at android.os.Handler.dispatchMessage(Handler.java:92)
03-25 20:20:23.024: D/MediaPlayer(24659):   at android.os.Looper.loop(Looper.java:137)
03-25 20:20:23.024: D/MediaPlayer(24659):   at android.app.ActivityThread.main(ActivityThread.java:5419)
03-25 20:20:23.024: D/MediaPlayer(24659):   at java.lang.reflect.Method.invokeNative(Native Method)
03-25 20:20:23.024: D/MediaPlayer(24659):   at java.lang.reflect.Method.invoke(Method.java:525)
03-25 20:20:23.024: D/MediaPlayer(24659):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
03-25 20:20:23.024: D/MediaPlayer(24659):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
03-25 20:20:23.024: D/MediaPlayer(24659):   at dalvik.system.NativeStart.main(Native Method)
03-25 20:20:23.024: D/MediaPlayer(24659): Couldn't open file on client side, trying server side
Mike Baxter
  • 6,868
  • 17
  • 67
  • 115

2 Answers2

4

You cannot set the ringtone from resource.

RingtoneManager.setActualDefaultRingtoneUri expects the ringtone file in the device and the Uri should be from content resolver.

 File ring = new File("pathOfYourFile");
 Uri path = MediaStore.Audio.Media.getContentUriForPath(ring.getAbsolutePath());
 RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE,path);

Also make sure to add permission

 <uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>
Libin
  • 16,967
  • 7
  • 61
  • 83
3

please try the solution in this question here

File file = new File(Environment.getExternalStorageDirectory(),
                "/myRingtonFolder/Audio/");
        if (!file.exists()) {
            file.mkdirs();
        }

        String path = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/myRingtonFolder/Audio/";

        File f = new File(path + "/", name + ".mp3");

        Uri mUri = Uri.parse("android.resource://"
                + context.getPackageName() + "/raw/" + name);
        ContentResolver mCr = context.getContentResolver();
        AssetFileDescriptor soundFile;
        try {
            soundFile = mCr.openAssetFileDescriptor(mUri, "r");
        } catch (FileNotFoundException e) {
            soundFile = null;
        }

        try {
            byte[] readData = new byte[1024];
            FileInputStream fis = soundFile.createInputStream();
            FileOutputStream fos = new FileOutputStream(f);
            int i = fis.read(readData);

            while (i != -1) {
                fos.write(readData, 0, i);
                i = fis.read(readData);
            }

            fos.close();
        } catch (IOException io) {
        }
        ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, f.getAbsolutePath());
        values.put(MediaStore.MediaColumns.TITLE, name);
        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
        values.put(MediaStore.MediaColumns.SIZE, f.length());
        values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
        values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
        values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
        values.put(MediaStore.Audio.Media.IS_ALARM, true);
        values.put(MediaStore.Audio.Media.IS_MUSIC, true);

        Uri uri = MediaStore.Audio.Media.getContentUriForPath(f
                .getAbsolutePath());
        Uri newUri = mCr.insert(uri, values);

        try {
            RingtoneManager.setActualDefaultRingtoneUri(context,
                    RingtoneManager.TYPE_RINGTONE, newUri);
            Settings.System.putString(mCr, Settings.System.RINGTONE,
                    newUri.toString());
        } catch (Throwable t) {

        }
Community
  • 1
  • 1
wael
  • 444
  • 4
  • 11
  • Check this link http://stackoverflow.com/questions/34764394/set-as-default-ringtone-from-raw-folder-programmatically-in-android – Misbah Farooqi May 22 '16 at 06:59