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

AndroidAudioShare

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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

Solution 2 - 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");

Solution 3 - Android

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.

Solution 4 - Android

Here are some methods that might help someone:

    public Uri getRawUri(String filename) {
        return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + File.pathSeparator + File.separator + getPackageName() + "/raw/" + filename);
    }
    public Uri getDrawableUri(String filename) {
        return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + File.pathSeparator + File.separator + getPackageName() + "/drawable/" + filename);
    }
    public Uri getMipmapUri(String filename) {
        return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + File.pathSeparator + File.separator + getPackageName() + "/mipmap/" + filename);
    }

Just call the method like this:

Uri rawUri = getRawUri("myFile.filetype");

Solution 5 - Android

One line Answer

RingtoneManager.getRingtone(getContext(),Uri.parse("android.resource://com.my.package/" + R.raw.fileName)).play();

Solution 6 - Android

Here is a clean way to get any resource,

 Uri.parse(String.format("android.resource://%s/%s/%s",this.getPackageName(),"resource_folder_name","file_name"));

Solution 7 - Android

Try saving the file to your SDCard and maybe then send the new URI,

 public boolean saveAs(int resSoundId){
 byte[] buffer = null;
 InputStream fIn = getBaseContext().getResources().openRawResource(resSoundId);
 int size=0;

 try {
  size = fIn.available();
  buffer = new byte[size];
  fIn.read(buffer);
  fIn.close();
 } catch (IOException e) {
  // TODO Auto-generated catch block
  return false;
 }

 String path = "/sdcard/yourapp/temp/";
 String filename = "filename"+".mp3";

 boolean exists = (new File(path)).exists();
 if (!exists){new File(path).mkdirs();}

 FileOutputStream save;
 try {
  save = new FileOutputStream(path+filename);
  save.write(buffer);
  save.flush();
  save.close();
 } catch (FileNotFoundException e) {
  // TODO Auto-generated catch block
  return false;
 } catch (IOException e) {
  // TODO Auto-generated catch block
  return false;
 }    

Remember to delete the file after the email is sent successfully.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKartik DomadiyaView Question on Stackoverflow
Solution 1 - AndroidKartik DomadiyaView Answer on Stackoverflow
Solution 2 - AndroidAndrew SamView Answer on Stackoverflow
Solution 3 - AndroidDewey ReedView Answer on Stackoverflow
Solution 4 - Androiduser9566632View Answer on Stackoverflow
Solution 5 - AndroidRadeshView Answer on Stackoverflow
Solution 6 - AndroidRemarioView Answer on Stackoverflow
Solution 7 - AndroidRenoView Answer on Stackoverflow