1

I am developing an app that allows to share an audio file with whatsapp.

I'm using this code:

Uri uri = Uri.parse("android.resource://" + getPackageName() + "/raw/song.mp3");
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("audio/mp3");
waIntent.setPackage("com.whatsapp");
waIntent.putExtra(Intent.EXTRA_STREAM, uri);
try {
    startActivity(waIntent);
}catch (android.content.ActivityNotFoundException ex){
    Toast.makeText(Fung.this,"Please, install Whatsapp", Toast.LENGTH_LONG).show();
}

But when whatsapp starts and I try send to a person, this error is displayed: "Fail to share, please try again".

When I change this code to send a text, it work, but not with audio file. Someone have anyone idea why this happens and how fix it?

Aleks G
  • 56,435
  • 29
  • 168
  • 265

1 Answers1

0

I suspect that the reason for the error is that the file you're trying to share is inside your app and WhatsApp simply doesn't have permission to read it. You cannot simply grant this permission either (you can't selectively grant permission to other apps to access resources of your app), therefore you'll need to copy this file somewhere on SD card - and then use the URI of the file on the SD card to send the intent to WhatsApp.

UPDATE: Have a look at this question for details on how do copy file.

Community
  • 1
  • 1
Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • I'm searching but not find how save an audio file on internal storage, just text file. Do you have any idea? – lidia rocha Nov 21 '16 at 02:27
  • @lidiarocha There's no difference between text and binary (audio in your case) file. I updated my answer with a link to a question/answer on how to do this. – Aleks G Nov 21 '16 at 10:26