Search

Thursday, July 16, 2009

Ringtone picker in android with Intent and URI sample code

Recently wanted to show all audio files on sdcard and phone that are set as ring tone and get the path of the file and i learn't that we have to call RingtoneManager. ACTION _RINGTONE_ PICKER intent activity and after a loads of trial and errors on bits of pieces of sample codes found on internet i was finally able to make it work and i thought of sharing with you and as an archive for my future reference below is the sample code.

Call the ring tone picker activity as given below

String uri = null;
Intent intent = new Intent( RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra( RingtoneManager.EXTRA_RINGTONE_TYPE,
RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra( RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
if( uri != null)
{
intent.putExtra( RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,
Uri.parse( uri));
}

else
{
intent.putExtra( RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,
(Uri)null);
}
startActivityForResult( intent, Set_Ringtone);


On onActivityResult use the below code to get the path

if (resultCode == RESULT_OK) {
Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if (uri != null) {
String ringTonePath = uri.toString();
}
}


and between remember if you pick or select "Silent" you will get "null" in URI as path the above code worked well for me if you have any problem or getting error feel free to contact me by leaving email by clicking icon at the bottom of this page

15 comments:

Anonymous said...

Thank you very much. I'll give it a try

Anonymous said...

Works nicely, thanks mucho!

Nagendra Anisetti said...

Nice example...
In the above code always silent is shown as checked though we selected another ringtone can any one solve that problem.

Nagendra Anisetti said...

Hi...
Above code is working nicely,but how to show music files or ringtones that are available in sd card into ringtone picker.Can any one solve this?

Dipak Keshariya said...

this is worked but problem is ringTonePath is always null.

Anonymous said...

I'm having the same problem as Dipak, is there a related permission we need to get useful data back?

Anonymous said...

This returns the id inside the RingtoneManager Database not actual file path..

I play sound via Soundpool (file path needed).

Any way i can get this path?

Anonymous said...

Great stuff...

All works great but i am trying to change the default sound with what has been returned

For example

RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE, soundUri);

Everywhere i look it says this is correct.. Am i doing something wrong?

Anonymous said...

Thanks alot!

Works like a charm :)

Unknown said...

Thank you. very useful
Chaim

Unknown said...
This comment has been removed by the author.
Unknown said...

" content://settings/system/ringtone "
getting this as a same path for all the ringtone ?
is it right ? will it work ?
i mean how come it has same path for different ringtone ?

Anonymous said...

thanks sir
how to save picked ringtone?

Anonymous said...

if i have select the silent tone error will occur why?

tibi said...

where does this come from?

Set_Ringtone

in the line:
startActivityForResult( intent, Set_Ringtone);

Post a Comment

Other Interesting Articles



Related Article Widget by Yogith

Search Powered by Google