Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing audio file

I'm trying to make a button for sharing an audio file. This is not working. First I tried to send the file right from my raw folder without copying it to the card of the phone. That didn't solve my problem. The second thing I tried, is saving the file to the phone and then share it. The part that saves the file to the phone works now, but when I try to share the audio file to another device all the compatible apps crash (Whatsapp, Gmail, etc).

This is my code:

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

By the way, the audio file is a .ogg file. I hope that those apps work with that type of files. If not I should convert it to .mp3.

Thanks in advance!

like image 361
Wannabe Avatar asked Sep 08 '13 17:09

Wannabe


People also ask

How do I share an audio file privately?

Via the providers Fidbak. audio,and Soundcloud you can upload a limited number of files for free and listen to them via a web player. All Companies offer the service to download the files or send them to other people via a private link. Wetransfer, Google Drive, PCloud, Megacloud and Dropbox are other popular options.

How can I send an audio file to another phone?

Go to your “File Manager” application or open the music app of your phone. Select the music files you intend to move and tap on “Share.” Pick “Bluetooth” from the sharing option and choose your Bluetooth device. Tap on “Accept” from the other Android phone and let the file sharing begin.


3 Answers

Oké, found out what I did wrong. For people who have the same problem, this is how I solved it:

I forgot to parse the String to an uri. Thats the only line of code I had to add. Uri uri = Uri.parse(sharePath);

Here is the full rest:

    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"));

Also do not forget to add permission WRITE_EXTERNAL_STORAGE otherwise you'll get an error while running your application.

like image 127
Wannabe Avatar answered Oct 12 '22 23:10

Wannabe


This is my way.

Uri uri = Uri.parse("file://" + sharePath);
like image 40
Chanh Avatar answered Oct 12 '22 23:10

Chanh


Below code worked for me

    SoundFiles soundFiles=.....
    String FullFilePath=soundFiles.getPath();
    Uri uri = Uri.fromFile(new File(FullFilePath));
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("audio/*");
    share.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(share, "Share Sound File"));
like image 23
Abhishek Kumar Avatar answered Oct 13 '22 00:10

Abhishek Kumar