Get Uri of .Mp3 File Stored in Res/Raw Folder in Android

Get URI of .mp3 file stored in res/raw folder in 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

android - how to get path of mp3 stored in raw folder

Is it possible to get file path by using R.raw.mp3file

No, because it is not a file on the device. It is a file on the hard drive of your development machine. On the device, it is merely an entry in an APK file.

Either:

  • Do whatever you are trying to do some other way that does not involve a file path, or

  • Use openInputStream() on a Resources object (you can get one from any Context via getResources()), and use Java I/O to copy the contents of that resource to a local file, such as on internal storage

How can I get my URI of .mp3 file stored in res/raw folder in android

As @CommonsWare Said Very few apps support the android:resource scheme
To make your app compatible with all apps.

You have to copy your raw resource to Internal Storage by this method

private String CopyRAWtoSDCard(int raw_id,String sharePath) throws IOException {
InputStream in = getResources().openRawResource(raw_id);
FileOutputStream out = new FileOutputStream(sharePath);
byte[] buff = new byte[1024];
int read = 0;
try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
} finally {
in.close();
out.close();
}
return sharePath;
}

Replace your method by below code

  public void shareAchieve() {
Intent shareAchievement = new Intent(Intent.ACTION_SEND);
shareAchievement.setType("audio/*");

String sPath= null;
try {
sPath = CopyRAWtoSDCard(R.raw.filename, Environment.getExternalStorageDirectory()+"/filename.mp3");
} catch (IOException e) {
e.printStackTrace();
}
Uri uri = Uri.parse(sPath);

shareAchievement.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareAchievement.putExtra(Intent.EXTRA_STREAM,uri);
startActivity(Intent.createChooser(shareAchievement, "Teile Längstes Achievement"));
}

Also add

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Get Uri from file in either assets or res/raw

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

sing the resource id, the format is:

"android.resource://[package]/[res id]"

Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/" + R.raw.myvideo);

or, using the resource subdirectory (type) and resource name (filename without extension), the format is:

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

Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/raw/myvideo");

Share .mp3 file located in /res/raw/ to WhatsApp

Finally I managed to solve the problem by mixing CommonsWare answer, this answer and this one. Thanks to all in advance!

Step by step solution:

First, let's start with some XML.

  1. Configure AndroidManifest.xml and add the following lines inside the <application> section. I put them in between my </activity> and </application> closing tags, but please take this placement as my personal choice: it might not work for you depending on your manifest layout.

AndroidManifest.xml

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>

As I am using API 29, I'm AndroidX libraries. In case you want to use it too consider running the AndroidX migration wizard by clicking Refactor > Migrate to AndroidX... in Android Studio at your own risk.


  1. Now create a file in /res/xml with the name file_paths.xml and fill it as follows:

file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
<cache-path name="my_sounds" path="/"/>
</paths>

Note the my_sounds name is arbitrary. The / in the path field is where in the cache path your file will be stored. I just let it like that for the sake of ease. Should you not want to use a cache path, here is the complete list of available tags you can use.

Now we will head back to Java and start coding the method that will handle the sharing. First of all we need to copy the file in our resource folder to a File object. This File, however, needs to be pointing to a path created by the File Provider we configured in the XML part. Let's divide the tasks:

  1. Create an InputStream with your file's data and fill a File with it with an auxiliary procedure.

public void handleMediaSend(int position)

File sound;
try {
InputStream inputStream = getResources().openRawResource(sounds.get(position).getSound()); // equivalent to R.raw.yoursound
sound = File.createTempFile("sound", ".mp3");
copyFile(inputStream, new FileOutputStream(sound));
} catch (IOException e) {
throw new RuntimeException("Can't create temp file", e);
}

Auxiliary procedure:

private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1)
out.write(buffer, 0, read);
}

Now your resource has been successfully transferred to a cached directory (use the debugger to see which one) inside your Internal Storage.


  1. Get an Uri and share the File.
final String AUTHORITY = BuildConfig.APPLICATION_ID + ".provider";
Uri uri = getUriForFile(getApplicationContext(), AUTHORITY, sound);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/mp3"); // or whatever.
share.putExtra(Intent.EXTRA_STREAM, uri);
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(share, "Share"));

Putting it all together we will end up with this method:

Full method

public void handleMediaSend(int position) { // Depends on your implementation.
File sound;
try {
InputStream inputStream = getResources().openRawResource(sounds.get(position).getSound()); // equivalent to R.raw.yoursound
sound = File.createTempFile("sound", ".mp3");
copyFile(inputStream, new FileOutputStream(sound));
} catch (IOException e) {
throw new RuntimeException("Can't create temp file", e);
}

final String AUTHORITY = BuildConfig.APPLICATION_ID + ".provider";
Uri uri = getUriForFile(getApplicationContext(), AUTHORITY, sound);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/mp3"); // or whatever.
share.putExtra(Intent.EXTRA_STREAM, uri);
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(share, "Share"));
}


Related Topics



Leave a reply



Submit