Swarup Donepudi Swarup Donepudi - 10 days ago 4
Java Question

Sharing the audio file located on an url with other apps in android

My android application has mp3 files sitting on the amazon s3 bucket. I have an URL to access that audio clip. I am able to play the audio clip using MediaPlayer by passing the URL to the data source of the media player.

I am creating an application which lets the user share audio clips from my app to other IM apps like whatsapp. So, I will provide a share widget on the Activity and upon cliking on that widget then whatsapp should be opened and the user should be able to select a contact to which he wants to share the audio clip.

For this I need to download the audio clip to local storage system and then share the file with other app using ContentURI. However I am unable to figure out what is the best way to do it.

As per Android documentation the below code canbe used to send binary files:

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));


I am assuming that audio files are binary files. So, I am using the below code to send the audio clip.

Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, uriToImage);
intent.setType("audio/mpeg3");
startActivity(intent);


Looks like the only piece I am missing here is "uriToImage". Can anyone help me understand how to get the "uriToImage" for the resource located at a URL. ?

Updated the code as per CommonsWare's comment. Below is the updated code:

Intent intent = new Intent(android.content.Intent.ACTION_SEND);
Uri contentUri = Uri.fromFile(new File(clipAudioUrl));
intent.putExtra(Intent.EXTRA_STREAM, contentUri);
intent.setType("audio/mpeg");
intent.setPackage("com.whatsapp");
startActivity(intent);


Upon touching the share widget Watsapp is being opened directly(Which is what I want), however the share is familing with the error "Share failed". I am assuming that it is because of the uri for which I have used the below code:

Uri contentUri = Uri.fromFile(new File(clipAudioUrl));


As per CommonsWare's comment, whatsapp also expect the URI to be either in the format "file:\" or "content:\"

Could you please help me convert the URL in the format of either "file:\" or "content:\". Thank you.

Answer

The solution is pretty much based on the suggestions from CommonsWare. However In my case I did not use FileProvder to send the file. Instead I used the below code to make it work.

public void onClick(View v) {
                    final Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
                    String audioClipFileName="shoutout.mp3";
                    shareIntent.setType("audio/mp3");
                    shareIntent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.parse("file://"+"/sdcard/"+audioClipFileName));
                    shareIntent.setPackage("com.whatsapp");
                    startActivity(Intent.createChooser(shareIntent, "Share Audio Clip"));
                }

To my surprise I actually discovered that my question is an outright duplicate of Intent.ACTION_SEND Whatsapp.

My solution which worked for me was from the answer for the above question.

Comments