0

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.

Swarup Donepudi
  • 984
  • 1
  • 11
  • 23
  • 1
    "For instance when the user clicks on an image the audio clip should be posted to watsapp" -- what image? What does WhatsApp have to do with this image? – CommonsWare Apr 26 '15 at 10:54
  • I have updated the question. Could you please help me here. I need a solution for this real quick. – Swarup Donepudi Apr 26 '15 at 11:02

2 Answers2

0

I will provide a share widget on the Activity and upon cliking on that widget then whatsapp should be opened

Only if the user chooses WhatsApp. Please do not assume that all users of your app will have WhatsApp installed or will want to use WhatsApp for everything in your app.

For this I need to download the audio clip to local storage system and then share the file with other app using ContentURI.

Technically, you could use your S3 URL with ACTION_SEND, though that implies that the URL is public.

Otherwise, download the file using whatever you want (AWS SDK, HttpUrlConnection, OkHttp, etc.) to internal storage (e.g., getCacheDir()), then use FileProvider to serve it to other apps. FileProvider can give you the Uri to use with ACTION_SEND.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Yes the URL is public. I dont have much time to explore how to use FileProvider. Is it possible to share the file with out having to download it at all? – Swarup Donepudi Apr 26 '15 at 11:10
  • @SwarupDonepudi: Just use the URL to the S3 entry. – CommonsWare Apr 26 '15 at 11:11
  • For sending text I would use intent.putExtra(Intent.EXTRA_TEXT, "Textmessage"); But for audio clip stored at a URL location what should I use? I am using the below code now. Intent i = new Intent(android.content.Intent.ACTION_SEND); i.putExtra("Audio Content should go here aint it?"); i.setType("plain/text"); startActivity(i); – Swarup Donepudi Apr 26 '15 at 11:16
  • @SwarupDonepudi: First, use actual MIME types. Plain text is `text/plain`, not `plain/text`. Second, use the MIME type for the actual data format. There seems to be a debate as to whether `audio/mpeg` or `audio/mp3` is the right one for MP3 files; you may wind up using `*/*` or `audio/*`. Third, use `EXTRA_STREAM` instead of `EXTRA_TEXT`, supplying a `Uri` based on your URL. Now, there are probably some apps that will be expecting `content://` or `file://` `Uri` schemes here, instead of `http`, and so this approach may not work for all apps. – CommonsWare Apr 26 '15 at 11:40
0

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.

Swarup Donepudi
  • 984
  • 1
  • 11
  • 23