Create mp3 ringtone From res/raw in Android Programmatically

Known:

  • Android Studio with SDK 28
  • Mobile Phone Lenovo with SDK 21

Wanted

I want to make mp3 ringtone from /res/raw folder

Question:

How to do it … ?

Answer:

The schema is like image below:

The Code

private void setRingTone() {

String lokasi = Environment.getExternalStorageDirectory().getAbsolutePath()+"/media/ringtone";
       
  File dirLok = new File(lokasi);
  if(!dirLok.exists())
     dirLok.mkdir();
  
 filePath = new File(lokasi,"filename.mp3");

if (!filePath.exists()) 
try {
  int idRaw = getBaseContext().getResources().getIdentifier("filename", "raw", getPackageName());

    InputStream is = getResources().openRawResource(idRaw);
          
    int size = is.available();
    byte[] buffer = new byte[size];
    is.read(buffer);
    is.close();

    FileOutputStream fos = new FileOutputStream(filePath);
    fos.write(buffer);
    fos.close();
  } catch (Exception e) {
       throw new RuntimeException(e);
  }

        ContentValues content = new ContentValues();
        content.put(MediaStore.MediaColumns.DATA,filePath.getAbsolutePath());
        content.put(MediaStore.MediaColumns.TITLE, "filename");
        content.put(MediaStore.MediaColumns.SIZE, filePath.length());
        content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
        content.put(MediaStore.Audio.Media.ARTIST, "MR");
        content.put(MediaStore.Audio.Media.DURATION, 3000);//in milisecond
        content.put(MediaStore.Audio.Media.IS_RINGTONE, true);
        content.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
        content.put(MediaStore.Audio.Media.IS_ALARM, false);
        content.put(MediaStore.Audio.Media.IS_MUSIC, false);


  try {
    Uri uri = MediaStore.Audio.Media.getContentUriForPath(filePath.getAbsolutePath());
            
    Uri newUri = this.getContentResolver().insert(uri, content);    
    RingtoneManager.setActualDefaultRingtoneUri(this.getBaseContext(), 
    RingtoneManager.TYPE_RINGTONE, newUri);
      
    Toast.makeText(this,"Ringtone already set", Toast.LENGTH_SHORT).show();
        
  } catch (Exception e) {
            e.printStackTrace();
  }
}

Explain:

  • lokasi variable is to get internal storage directory
  • filepath is mp3 location inside internal storage
  • if filepath not exist then copy from res/raw folder
  • idRaw is the id to get like R.raw.filename
  • ContentValues is values for ContentResolver
  • ContentResolver is a class used to access content model
  • RingtoneManager.setActualDefaultRingtoneUri is to set Uri as default ringtone

Permission

Open AndroidManifest.xml and add this permissions:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.WRITE_SETTINGS"
        tools:ignore="ProtectedPermissions" />

Manifest Attribut

Add this attribut to <Manifest ……. >

xmlns:tools="http://schemas.android.com/tools"

Thanks for reading

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *