How to set ringtone from raw folder in Android programmatically

I tried this code its working fine to load the sound file to ringtones directory.I can manually select the sound from popup ..but its fail to set as default ringtone programmatically.plz help me to set the sound as default ringtone programmatically

        setContentView(R.layout.activity_main);
      String exStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
     path=(exStoragePath +"/media/alarms/");
            // saveas();
        saveas(RingtoneManager.TYPE_RINGTONE);

    }

    // In this method, we need to copy the mp3 file to the sd card location from
    // where android picks up ringtone files
    // After copying, we make the mp3 as current ringtone
public boolean saveas(int type) {
enter code here
    byte[] buffer = null;
    InputStream fIn = getBaseContext().getResources().openRawResource(
            R.raw.sound);
    int size = 0;

    try {
        size = fIn.available();
        buffer = new byte[size];
        fIn.read(buffer);
        fIn.close();
    } catch (IOException e) {
        return false;
    }


    String filename = "sound";

    boolean exists = (new File(path)).exists();
    if (!exists) {
        new File(path).mkdirs();
    }

    FileOutputStream save;
    try {
        save = new FileOutputStream(path + filename);
        save.write(buffer);
        save.flush();
        save.close();
    } catch (FileNotFoundException e) {
        return false;
    } catch (IOException e) {

        return false;

    }

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + path + filename + ".mp3"
            + Environment.getExternalStorageDirectory())));
enter code here
    File k = new File(path, filename);

    ContentValues values = new ContentValues();
    long current = System.currentTimeMillis();
    values.put(MediaStore.MediaColumns.DATA, path + filename  );
    values.put(MediaStore.MediaColumns.TITLE,  filename );
    values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
    values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/mp3");

    //new
    values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");
    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);

RingtoneManager.getRingtone(context,this.getContentResolver()
    .insert(MediaStore.Audio.Media.getContentUriForPath(k
            .getAbsolutePath()), values));



    return true;

}

asked Jan 13, 2016 at 10:36

How to set ringtone from raw folder in Android programmatically

1

Remove this code RingtoneManager.getRingtone(context,this.getContentResolver() .insert(MediaStore.Audio.Media.getContentUriForPath(k .getAbsolutePath()), values));

and put these lines code before return true and its work

 Uri newUri = this.getContentResolver()
            .insert(MediaStore.Audio.Media.getContentUriForPath(k
                    .getAbsolutePath()), values);
    RingtoneManager.setActualDefaultRingtoneUri(MainActivity.this, RingtoneManager.TYPE_RINGTONE, newUri);

answered Jan 14, 2016 at 17:36

 try {
                Uri notification = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.phone_funny_bell);
                Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
                r.play();

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        r.stop();
                    }
                }, 3000);
            } catch (Exception e) {
                e.printStackTrace();
            }

Use this one. This will work.

place above code on MyFirebaseMessagingService inside this onMessageReceived

answered Oct 24, 2019 at 10:48

How to set ringtone from raw folder in Android programmatically

PeterPeter

5275 silver badges15 bronze badges

    • Actions

      Automate any workflow

    • Packages

      Host and manage packages

    • Security

      Find and fix vulnerabilities

    • Codespaces

      Instant dev environments

    • Copilot

      Write better code with AI

    • Code review

      Manage code changes

    • Issues

      Plan and track work

    • Discussions

      Collaborate outside of code

    • Explore
    • All features
    • Documentation
    • GitHub Skills
    • Changelog

    • By Plan
    • Enterprise
    • Teams
    • Compare all
    • By Solution
    • CI/CD & Automation
    • DevOps
    • DevSecOps
    • Case Studies
    • Customer Stories
    • Resources

    • GitHub Sponsors

      Fund open source developers

    • The ReadME Project

      GitHub community articles

    • Repositories
    • Topics
    • Trending
    • Collections

  • Pricing