Skip to content Skip to sidebar Skip to footer

Set Raw Resource As Ringtone In Android

In my android application, I want to set audio file from my raw folder as a ringtone. For that i wrote below code, but its not working. Please help me to solve this issue. Thank yo

Solution 1:

The following code solved my problem:

String name = "your_raw_audio_name";
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 = newbyte[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) {}

Solution 2:

In case you want to set the same res as ringtone again, the below modification might be helpful

Uriuri= MediaStore.Audio.Media.getContentUriForPath(f
                     .getAbsolutePath());
mCr.delete(uri, MediaStore.MediaColumns.DATA + "=\"" + f.getAbsolutePath() + "\"", null);
UrinewUri= mCr.insert(uri, values);

Solution 3:

This is the code i used: very usefull to me:)

StringexStoragePath= Environment.getExternalStorageDirectory().getAbsolutePath();
 String path=(exStoragePath +"/media/alarms/"); 

    saveas(RingtoneManager.TYPE_RINGTONE);

 sendBroadcast(newIntent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+path+filename+".mp3"
          + Environment.getExternalStorageDirectory()))); 




 Filek=newFile(path, filename);

  ContentValuesvalues=newContentValues(4);
longcurrent= 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/3gpp");

//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, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);  

// Insert it into the databasethis.getContentResolver()
.insert(MediaStore.Audio.Media.getContentUriForPath(k
  .getAbsolutePath()), values);

Or refer on this complete tutorial

HAPPY CODING!

Post a Comment for "Set Raw Resource As Ringtone In Android"