Setting Raw Resource As A Ringtone
I have read these 2 posts link1 and this one link2 but code does not seem to work for me. Here is my code: File newSoundFile = new File('/sdcard/media/ringtone', 'myringtone.oog');
Solution 1:
Put your ringtone sound in assets folder and use this code
Uri path = Uri.parse("android.resource://yourpackagename/raw/yoursoundfile")
RingtoneManager.setActualDefaultRingtoneUri(
getApplicationContext(), RingtoneManager.TYPE_RINGTONE,
path);
Log .i("TESTT", "Ringtone Set to Resource: "+ path.toString());
RingtoneManager.getRingtone(getApplicationContext(), path)
.play();
Add this permission to AndroidManifest.xml:
<uses-permissionandroid:name="android.permission.WRITE_SETTINGS" />
Solution 2:
This is the code i used! very helpfull to me especially this tutorial !
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);
HAPPY CODING!
Post a Comment for "Setting Raw Resource As A Ringtone"