Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have try with something and its work for me. Hope it will also help you.</p> <p>I am fetching all the available songs in device with below method:</p> <pre><code>public static long [] getAllSongs(Context context) { //System.out.println("In get All Songs Method"); Cursor c = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[] {MediaStore.Audio.Media._ID}, MediaStore.Audio.Media.IS_MUSIC +"!= 0", null, MediaStore.Audio.Media.TITLE + " ASC"); try { if (c == null || c.getCount() == 0) { return null; } int len = c.getCount(); long [] list = new long[len]; for (int i = 0; i &lt; len; i++) { c.moveToNext(); list[i] = c.getLong(0); //System.out.println("ID IS: "+c.getLong(0)); } //System.out.println("Total Songs are: ); // ALL_SONGS_ID = list; return list; } finally { if (c != null) { c.close(); } } } </code></pre> <p>After fetching that songs i am selecting the sound file and set it as Ringtone with below code:</p> <pre><code> static void setRingtone(Context context, long id) { ContentResolver resolver = context.getContentResolver(); // Set the flag in the database to mark this as a ringtone Uri ringUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id); try { ContentValues values = new ContentValues(2); values.put(MediaStore.Audio.Media.IS_RINGTONE, "1"); values.put(MediaStore.Audio.Media.IS_ALARM, "1"); resolver.update(ringUri, values, null, null); } catch (UnsupportedOperationException ex) { // most likely the card just got unmounted Log.e("Music app", "couldn't set ringtone flag for id " + id); return; } String[] cols = new String[] { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.TITLE }; String where = MediaStore.Audio.Media._ID + "=" + id; Cursor cursor = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, cols, where , null, null); try { if (cursor != null &amp;&amp; cursor.getCount() == 1) { // Set the system setting to make this the current ringtone cursor.moveToFirst(); Settings.System.putString(resolver, Settings.System.RINGTONE, ringUri.toString()); String message = context.getString(R.string.ringtone_set, cursor.getString(2)); Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); } } finally { if (cursor != null) { cursor.close(); } } </code></pre> <p>}</p> <p>In above method id is the Song id of selected song.</p> <p>Hope this will help you. Be free to ask if you dont get the solution.</p> <p>Enjoy Coading... :)</p>
 

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