2

My app use MP3 files with MediaPlayer, i want to make a button than will share an MP3 file to whatsapp.

my code is this:

    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    String audioClipFileName="bell.mp3";
    sendIntent.setType("audio/mp3");
    sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+"/sdcard/"+audioClipFileName));
    startActivity(sendIntent);

but its not working.why is not working?how can i solve this issue?

sasikumar
  • 12,540
  • 3
  • 28
  • 48
Adam
  • 299
  • 1
  • 4
  • 19
  • Did you copy the file out to the stated path? If not, what makes you think that it will be there? Also, **never hardcode root paths**. There are many, many devices for which `/sdcard` will not work. – CommonsWare Jun 18 '15 at 12:20
  • the path can be res/raw/xxx.mp3 ? – Adam Jun 18 '15 at 12:24
  • Shouldn't your extra files be in assets folder to begin with ? – Sanket Berde Jun 18 '15 at 12:25
  • those are files that i use in my project.. not user's files.. – Adam Jun 18 '15 at 12:28
  • try changing `startActivity(sendIntent)` to `startActivity(Intent.createChooser(sendIntent, "Share sound"));` – Strider Jun 18 '15 at 12:33
  • "the path can be res/raw/xxx.mp3" -- that is not a file on the Android device. So, I ask again: did you copy the file out to the stated path? Please show the code where you copied this raw resource out to the path designated by your `Uri.parse()` call. – CommonsWare Jun 18 '15 at 12:38
  • @ADAMCOHENHILLEL plz change `"file://"+"/sdcard/"` to `"file:///sdcard/"` there is no need for the **+** for two string types – Strider Jun 18 '15 at 12:50
  • i did not copy, how do i copy?or if you suggest a better solution – Adam Jun 18 '15 at 12:51

1 Answers1

1

You need use Environment and not hardcode path, for example, if your file is in sdcard root use a code like this:

File root = Environment.getExternalStorageDirectory().getPath();
String fname = "bell.mp3";
file = new File(root, fname);

Intent shareCaptionIntent = new Intent(Intent.ACTION_SEND);
shareCaptionIntent.setType("audio/mp3");
shareCaptionIntent.putExtra(Intent.EXTRA_TEXT, "YOURTEXT");
shareCaptionIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file.toString()));
startActivity(Intent.createChooser(shareCaptionIntent, "Share in:"));

If a resource use this:

Uri.parse("android.resource://com.my.package/raw/" + fname);

Or resource ID

Uri.parse("android.resource://com.my.package/" + R.raw.bell.mp3);

Any error please share logcat

Santiago
  • 1,744
  • 15
  • 23
  • and if my path is res/raw/xxx.mp3? – Adam Jun 18 '15 at 12:50
  • 1
    @ADAMCOHENHILLEL I update the answer, use Uri.parse("android.resource://com.my.package/raw/" + fname); for resources – Santiago Jun 18 '15 at 12:56
  • Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); String audioClipFileName="bell.mp3"; sendIntent.setType("audio/mp3"); sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://com.gruchka.guessthesound/raw/" + audioClipFileName)); startActivity(Intent.createChooser(sendIntent, "Get help with:")); – Adam Jun 18 '15 at 13:08
  • Or use resource ID, share logcat Uri.parse("android.resource://com.my.package/" + R.raw.bell.mp3); – Santiago Jun 18 '15 at 13:16
  • How can i add to the audio also text? – Adam Jun 18 '15 at 13:43
  • Try using Uri.parse("android.resource://com.my.package/raw/bell"); with .mp3 extension, the posted in answer is the way that I use. – Santiago Jun 18 '15 at 13:51
  • For text use shareCaptionIntent.putExtra(Intent.EXTRA_TEXT, "YOUR_TEXT"); – Santiago Jun 18 '15 at 13:54
  • This is what i did but the intent type is audio/mp3 and it is need to be something else – Adam Jun 18 '15 at 13:56