Get URI of .mp3 file stored in res/raw folder in android
This work like magic: Assuming the mp3 is saved in the raw folder as
notification_sound.mp3
then simple doing this works:
Uri uri=Uri.parse("android.resource://"+getPackageName()+"/raw/notification_sound");
Finally after searching a lot i found the solution.
If you want to get any resource URI then there are two ways :
Using Resource Name
Syntax :
android.resource://[package]/[res type]/[res name]
Example :
Uri.parse("android.resource://com.my.package/drawable/icon");
Using Resource Id
Syntax :
android.resource://[package]/[resource_id]
Example :
Uri.parse("android.resource://com.my.package/" + R.drawable.icon);
This were the examples to get the URI of any image file stored in drawable folder.
Similarly you can get URIs of res/raw folder.
In my case i used 1st way ie. Using Resource Name
Don't construct string on your own, use Uri.Builder
:
/**
* @param resourceId identifies an application resource
* @return the Uri by which the application resource is accessed
*/
internal fun Context.getResourceUri(@AnyRes resourceId: Int): Uri = Uri.Builder()
.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
.authority(packageName)
.path(resourceId.toString())
.build()
From AOSP-DeskClock.