Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>@Maxood</p> <p>The code from @Clive is what you need to set the ringtone. You will need the absolute path to the file, which you can't get from a raw resource.</p> <p>The solution is to get the resource file asset and write it to the sdcard 1st, before you give it to the content resolver for insertion.</p> <pre><code>File newSoundFile = new File("/sdcard/media/ringtone", "myringtone.oog"); Uri mUri = Uri.parse("android.resource://com.your.package/R.raw.your_resource_id"); ContentResolver mCr = app.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) { } </code></pre> <p>Then you can use the previously posted solution </p> <pre><code> ContentValues values = new ContentValues(); values.put(MediaStore.MediaColumns.DATA, newSoundFile.getAbsolutePath()); values.put(MediaStore.MediaColumns.TITLE, "my ringtone"); values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/oog"); 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); try { RingtoneManager.setActualDefaultRingtoneUri(getContext(), RingtoneManager.TYPE_RINGTONE, newUri); } catch (Throwable t) { Log.d(TAG, "catch exception"); } </code></pre> <p>Don't forget to write the the permission </p> <p><code>&lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt;</code> </p> <p>in your manifest</p> <p>hope this helps</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload