Abhilash Maurya
Abhilash Maurya

Reputation: 308

Sharing an Audio wav file

I want to share the audio file in android using but didn't found any working solution.

In My app the app records and save the audio file named as audio_1.wav each time in the app directory and then provide the option for sharing it. So always it share the file with same name(audio_1.wav).

Also checked that it is creating the file correctly. I tried to share the audio file with below code;

File f = new File(sharePath);
Uri uri = Uri.parse(f.getAbsolutePath());
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/*");
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Sound File"));

But while sharing and selecting any app it show unable to attach File.

Also Used FileProvider Class for Nougat Devices but it didn't worked as well. Checked multiple SO Link as well:

Sharing an audio file

Picking up an audio file android

Upvotes: 0

Views: 1330

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006799

With regards to the code that you have:

  • Replace Uri uri = Uri.parse(f.getAbsolutePath()); with Uri uri = Uri.fromFile(f);, to get a valid Uri

  • Use audio/wav, not audio/*, for the MIME type

Your code will crash on Android 7.0+ with a FileUriExposedException. Use FileProvider for that, where you use <external-path> in your metadata XML resource and add the FLAG_GRANT_READ_URI_PERMISSION flag to your Intent. See the documentation for more.

Upvotes: 3

Related Questions