Set ringtone using mp3 from res / raw folder

Well, I tried several ways and it still doesn't work. The path is more β€œcorrect” - this is (I think), I really need help, I'm crazy! :)

public void setRingtone(){ String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath(); String fileName = nombreActual+".mp3"; File newSoundFile = new File(baseDir, fileName); Uri mUri = Uri.parse("android.resource://com.genaut.ringtonelists/raw/"+sonidoActual);//sonidoActual (sound name without .mp3) ContentResolver mCr = 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(newSoundFile); 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, newSoundFile.getAbsolutePath()); values.put(MediaStore.MediaColumns.TITLE, nombreActual); values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3"); values.put(MediaStore.MediaColumns.SIZE, newSoundFile.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, false); Uri uri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile.getAbsolutePath()); Uri newUri = mCr.insert(uri, values); RingtoneManager.setActualDefaultRingtoneUri(Main.this, RingtoneManager.TYPE_RINGTONE, newUri); Log.d("AbsolutePath: ", newSoundFile.getAbsolutePath()); Log.d("DefaultUri: ", RingtoneManager.getActualDefaultRingtoneUri(Main.this, RingtoneManager.TYPE_RINGTONE).toString()); } 

I have rights to the manifest:

  <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

I tried several codes, tried to mix some steps (copying the file and installing the library ..), but no work. Im really stucks with this. With this code I can add my sound (Settings - Devices - Audio profiles - "Select profile" - Voice call ringtone), but it is not selected by default, and if I select it, it will not play sound: (

I do not see the sound file anywhere on the SD, but Log:

 08-05 03:20:22.362: D/AbsolutePath:(6277): /mnt/sdcard/Ding Ding Dong.mp3 08-05 03:20:22.364: D/DefaultUri:(6277): content://media/external/audio/media/15233 

This example: the file "Ding Ding Dong.mp3" and others that I copied are on the SD card, but with a size of 0 !! .

+4
source share
1 answer

Try to make this code all the best

 File k = new File(path, "mysong.mp3"); // path is a file to /sdcard/media/ringtone ContentValues values = new ContentValues(); values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath()); values.put(MediaStore.MediaColumns.TITLE, "My Song title"); values.put(MediaStore.MediaColumns.SIZE, 215454); values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3"); values.put(MediaStore.Audio.Media.ARTIST, "Madonna"); values.put(MediaStore.Audio.Media.DURATION, 230); 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); //Insert it into the database Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()); Uri newUri = main.getContentResolver().insert(uri, values); RingtoneManager.setActualDefaultRingtoneUri( myActivity, RingtoneManager.TYPE_RINGTONE, newUri ); 
+2
source

All Articles