how to make a button that share my res/raw/xxx.mp3 file

2.2k views Asked by At

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?

1

There are 1 answers

8
Santiago On BEST ANSWER

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