2

I am trying to share an audio file saved by my application. The file is added to the media store so all apps can access it.

Intent mediaScannerIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    Uri fileContentUri = Uri.fromFile(finalFile);
    mediaScannerIntent.setData(fileContentUri);
    this.sendBroadcast(mediaScannerIntent);

I use Intent.ACTION_SEND to share the files to other apps:

public void shareRecording(View view) {
    Intent i = new Intent(Intent.ACTION_SEND);
    i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    i.setType("audio/mp3");
    i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///" + recording.getFilePath()));
    try {
        startActivity(Intent.createChooser(i, "Share " + recording.getName()));
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(this, "There are no app installed to share your audio file.", Toast.LENGTH_SHORT).show();
    }
}

All working good with all apps like GMail, Yahoo mail, Whatsapp, ... except BBM, it gives access denied. What do I need to do to make it work on BBM ?

Thanks for any help.

UPDATE:

I used

Uri fileUri = Uri.parse("content://" + recording.getFilePath());

instead of

Uri fileUri = Uri.parse("file:///" + recording.getFilePath());

and it worked on BBM but not othe apps

So what is the difference between havin the URI parsed with: "file:///" and "content://" ? And how can I used this to get the share working for all apps ?

Error on BBM

Beshoy Fayez
  • 300
  • 5
  • 10

1 Answers1

2

The solution is to use the actual file to initialize the Uri object.

File recordingFile = new File(recording.getFilePath());
Uri fileUri = Uri.fromFile(recordingFile);

This worked with all the apps that can share a file.

Beshoy Fayez
  • 300
  • 5
  • 10