-1

I have a problem. I'd like to share my MP3 File to Whatsapp but it don't work! Here my Share Intent:

    public void shareAchieve() {
        Intent shareAchievement = new Intent();
        shareAchievement.setType("audio/*");
        shareAchievement.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://de.logtainment.ungesoundboard/" + R.raw.achieve + ".mp3"));
        startActivity(Intent.createChooser(shareAchievement, "Teile Längstes Achievement"));
    }

But it doesnt work! Sorry for my bad english i'm from germany.

Fahim
  • 12,198
  • 5
  • 39
  • 57
  • Very few apps support the `android:resource` scheme. If Whatapp does not support it, there is nothing you can really do to force them to do so. You will have better luck sharing a file rather than a resource. – CommonsWare Mar 08 '15 at 17:17

2 Answers2

1

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" />
IshRoid
  • 3,696
  • 2
  • 26
  • 39
0

You have missed adding /raw/ to the path and change your Intent like

change

Intent shareAchievement= new Intent();
shareAchievement.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://de.logtainment.ungesoundboard/" + R.raw.achieve + ".mp3"));

to

Intent shareAchievement= new Intent(Intent.ACTION_SEND);
shareAchievement.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://de.logtainment.ungesoundboard/raw/achieve.mp3"));
Fahim
  • 12,198
  • 5
  • 39
  • 57