4
Intent share = new Intent(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://" + ContextID.getPackageName() + "/" + ResourceID));
share.setType("audio/*");
ContextID.startActivity(Intent.createChooser(share, "Condividi il suono"));

The above code works fine with Gmail, while Whatsapp gives a toast message like "Share a file failed, please try it again"

Maybe i've the same problem of this guy: Intent.ACTION_SEND Whatsapp

But how can i temporarily copy my resources on sd card and then share them?

Community
  • 1
  • 1
filippo
  • 839
  • 2
  • 13
  • 25

2 Answers2

6
File dest = Environment.getExternalStorageDirectory();
InputStream in = ContextID.getResources().openRawResource(ResourceID);              

try 
{
    OutputStream out = new FileOutputStream(new File(dest, "lastshared.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) {}              

Intent share = new Intent(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory().toString() + "/lastshared.mp3"));
share.setType("audio/*");
ContextID.startActivity(Intent.createChooser(share, "Condividi il suono \"" + TheButton.getText() + "\""));
return true;

manifest:

<manifest ...>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    ...
</manifest>
filippo
  • 839
  • 2
  • 13
  • 25
  • 1
    Hi I'm using your code to send a `mp3` file through WhatsApp but I'm getting an error similar to `Failed to send, please try again`. Do you know how to overcome this? Thanks in advance – alvaro.delaserna Dec 17 '14 at 11:16
  • its working .. .if you try to share directly from assets, whatsapp will not allow you. – Ramesh_D Jun 19 '15 at 07:09
  • Grazie Mille!! well Done;) – Manza Feb 17 '16 at 21:13
  • you don't require WRITE_EXTERNAL_STORAGE, as it can be done via content provider. share your file via content provider instead of saving in external storage and then share – kartikag01 Sep 03 '16 at 20:06
0

please change this line from your code and you will be able to share write ("audio/mp3") as bellow instead ("audio/*")

share.setType("audio/mp3");

this is because share type for whatsapp doesn't support ("audio/*") or ("*/*")

Shubham
  • 1,205
  • 1
  • 12
  • 15