2

I'm trying to share an audio file from my res/raw folder. What I've done so far is:

Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.sound); //parse path to uri
Intent share = new Intent(Intent.ACTION_SEND); //share intent
share.setType("audio/*");
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share sound to"));

When I choose to share it on GMail, for example, it says something like "Failed to attach empty file". Looks like I'm not getting the right file path, so I'm basically sharing nothing. What am I doing wrong?

Any help would be much appreciated.

3 Answers 3

4

Copy the audio file from the resource to external storage and then share it:

InputStream inputStream;
FileOutputStream fileOutputStream;
try {
    inputStream = getResources().openRawResource(R.raw.sound);
    fileOutputStream = new FileOutputStream(
            new File(Environment.getExternalStorageDirectory(), "sound.mp3"));

    byte[] buffer = new byte[1024];
    int length;
    while ((length = inputStream.read(buffer)) > 0) {
        fileOutputStream.write(buffer, 0, length);
    }

    inputStream.close();
    fileOutputStream.close();
} catch (IOException e) {
    e.printStackTrace();
}

Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM,
        Uri.parse("file://" + Environment.getExternalStorageDirectory() + "/sound.mp3" ));
intent.setType("audio/*");
startActivity(Intent.createChooser(intent, "Share sound"));

Add WRITE_EXTERNAL_STORAGE permission to AndroidManifest.xml file:

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

What am I doing wrong?

Few apps handle android.resource Uri values correctly. Your choices are:

  1. Drop the feature, or

  2. Copy the data from the resource into a file, then use FileProvider, perhaps in conjunction with my LegacyCompatCursorWrapper, or

  3. Use my StreamProvider, which can serve raw resources directly, or

  4. Copy the data from the resource into a file, then use Uri.fromFile(), but this looks like it will stop working with the next version of Android, based on preliminary results from testing with the N Developer Preview

3
  • Does FileProvider work with internal files like the ones under the res/raw folder? If so, could you please give me some code example on how to do it? Thanks a lot! Mar 12, 2016 at 16:25
  • @MiquelPerez: "Does FileProvider work with internal files like the ones under the res/raw folder?" -- those are resources. They are files on your development machine. They are not files on the device. StreamProvider can stream resources directly; there is a sample app in the project repo that shows how to use it (albeit using assets/ rather than raw resources). Mar 12, 2016 at 16:32
  • Please see my recent answer to tue question. Thanks. Mar 12, 2016 at 17:36
0

EDIT: It was causing a NullPointException. This is what was I doing:

File dest = Environment.getExternalStorageDirectory();
                InputStream in = getResources().openRawResource(R.raw.sound);

try
{
    OutputStream out = new FileOutputStream(new File(dest, "sound.mp3"));
    byte[] buf = new byte[1024];
    int len;
    while ( (len = in.read(buf, 0, buf.length)) != -1){
        out.write(buf, 0, len);
    }
    in.close();
    out.close();

}catch (Exception e) {}


 final Uri uri = FileProvider.getUriForFile(Soundboard.this, "myapp.folagor.miquel.folagor", dest); //NullPointerException right here!!

 final Intent intent = ShareCompat.IntentBuilder.from(Soundboard.this)
                        .setType("audio/*")
                        .setSubject(getString(R.string.share_subject))
                        .setStream(uri)
                        .setChooserTitle(R.string.share_title)
                        .createChooserIntent()
                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET)
                        .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

  startActivity(intent);

The code was just fine. The only problem was that on the Manifest's permisions, I had "WRITE_EXTERNAL_STORAGE" instead of "android.permissions.WRITE_EXTERNAL_STORAGE". So I was not having permision to write in the external storage, which caused a FileNotFoundException due to the lack of permision. Now it works fine!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.