简体   繁体   中英

How can I share a sound to whatsapp in Android Studio?

I tried it like this:

shareBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {



                final Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
                shareIntent.setType("audio/mp3");
                shareIntent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.parse("file://com.example.aaron.sharetest/sound.mp3"));
                startActivity(Intent.createChooser(shareIntent, "Sound shared!"));

But this is not working, whatsapp tells me to try it again.

I also found this way in the answers of this stackoverflow question :

String sharePath = Environment.getExternalStorageDirectory().getPath()
        + "/Soundboard/Ringtones/custom_ringtone.ogg";
Uri uri = Uri.parse(sharePath);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/*");
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Sound File"));

What do I have to write instead of this in my case?:

Environment.getExternalStorageDirectory().getPath()
            + "/Soundboard/Ringtones/custom_ringtone.ogg" 

My sound.mp3 file is located in the raw folder

You can share Raw folder files using this code

name = file name

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
share.setType("audio/*");
 sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(
        "android.resource://"+ this.getPackageName()+"/raw/" + name
));
startActivity(Intent.createChooser(sharingIntent, "Share via"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM