Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get URI of .mp3 file stored in res/raw folder in android

I have many .mp3 files stored in res/raw folder.

I am getting URI of .mp3 file using following code.

Uri.parse("android.resource:///com.my.android.sharesound/"+resId); 

This returns : android.resource:///com.my.android.sharesound/2130968609

Now I am using this URI in creating Share Intent

    shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("android.resource://com.my.android.sharesound/"+resId));     startActivity(Intent.createChooser(shareIntent,"Share Sound"); 

When i select any Mail application eg. Gmail or YahooMail from the Share Intent, the mp3 file attached successfully. But it shows that 2130968609(as an attachment)

i dont want resourceId(2130968609) to appear.

I want to display fileName.mp3

How can i do that? Am i missing something ?

OR

Is there any other way to attach .mp3 file stored in res/raw to mail via Share Intent.

like image 749
Kartik Domadiya Avatar asked Nov 02 '11 05:11

Kartik Domadiya


People also ask

How do you find the absolute path of URI?

Uri uri = data. getData(); File file = new File(uri. getPath());//create path from uri final String[] split = file. getPath().

What is kept in res folder?

The raw (res/raw) folder is one of the most important folders and it plays a very important role during the development of android projects in android studio. The raw folder in Android is used to keep mp3, mp4, sfb files, etc. The raw folder is created inside the res folder: main/res/raw.


2 Answers

Finally after searching a lot i found the solution.

If you want to get any resource URI then there are two ways :

  1. Using Resource Name

    Syntax : android.resource://[package]/[res type]/[res name]

    Example : Uri.parse("android.resource://com.my.package/drawable/icon");

  2. 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

like image 52
Kartik Domadiya Avatar answered Sep 22 '22 03:09

Kartik Domadiya


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"); 
like image 20
Andrew Sam Avatar answered Sep 19 '22 03:09

Andrew Sam