Sharing audio file

こ雲淡風輕ζ 提交于 2019-11-27 13:48:57
Wannabe

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.

Chanh

This is my way.

Uri uri = Uri.parse("file://" + sharePath);

change this line and it will support whatsapp and other apps:

share.setType("audio/mp3");

because whatsapp does not support ("audio/*"); or ("*/*");

Send Audio file via WhatsApp:

We have to remember that the media file to share via WhatsApp must be stored in External Storage Directory, in this case you are going to send a file stored in /raw folder, so we have to make a copy and then make the intent to send this file via Whatsapp:

This is an example supposing to have a file /raw/jorgesys_sound.mp3

This are the methods to use

  private void sendWhatsAppAudio(){
        try {
           //Copy file to external ExternalStorage.
           String mediaPath = copyFiletoExternalStorage(R.raw.jorgesys_sound, "jorgesys_sound.mp3");

           Intent shareMedia = new Intent(Intent.ACTION_SEND);
            //set WhatsApp application.
            shareMedia.setPackage("com.whatsapp");
            shareMedia.setType("audio/*");
            //set path of media file in ExternalStorage.
            shareMedia.putExtra(Intent.EXTRA_STREAM, Uri.parse(mediaPath));
        startActivity(Intent.createChooser(shareMedia, "Compartiendo archivo."));
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Whatsapp no se encuentra instalado", Toast.LENGTH_LONG).show();
        }
    }

    private String copyFiletoExternalStorage(int resourceId, String resourceName){
        String pathSDCard = Environment.getExternalStorageDirectory() + "/Android/data/" + resourceName;
        try{
        InputStream in = getResources().openRawResource(resourceId);
        FileOutputStream out = null;
        out = new FileOutputStream(pathSDCard);
        byte[] buff = new byte[1024];
        int read = 0;
        try {
            while ((read = in.read(buff)) > 0) {
                    out.write(buff, 0, read);
            }
        } finally {
            in.close();
            out.close();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return  pathSDCard;
  }

With this methods you can send any file via WhatsApp.

Remember to have the permission:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

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

I'm using:

final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("audio/mp3");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory().toString() + "/breakfast.mp3"));
startActivity(Intent.createChooser(shareIntent, "Condividi attraverso:"));

but gmail say "Can't attach empty file"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!