Problem sending an audio by WhatsApp from the raw folder

0

When I send to Whatsapp an audio that is saved in the folder raw of my application, the file is sent but without format, that is, a file is sent without title and without the extension.

I have checked that file if I manually put the extension .mp3 I can hear it.

Code that I am using:

Intent compartiraudio = new Intent(Intent.ACTION_SEND);  
Uri uri = Uri.parse("android.resource://"+getApplicationContext().getPackageName()+"/raw/audio");

compartiraudio.setType("audio/,mp3"); //el tipo es un audio

compartiraudio.putExtra(Intent.EXTRA_STREAM, uri);

startActivity(Intent.createChooser(compartiraudio, "Comparte un archivo de audio"));

This is the final result in WhatsApp :

How can I send it as if it were an audio of WhatsApp ?

    
asked by Diego Garcia Diaz 07.04.2018 в 14:46
source

1 answer

0

I went crazy, but at the end I stumbled over the solution on the internet and I share them!

Any questions chew the link. link

//Mi boton que al mantener pulsado por un periodo va llamar al metodo compartir

        buttonRep25= (Button)findViewById(button25);
        buttonRep25.setOnLongClickListener(new View.OnLongClickListener() {

        public boolean onLongClick(View v) {

        //llamada al metodo donde le damos los valores (int a1 ,String a2)

            compartir(R.raw.futuru_por_pasado,"futuru_por_pasado.mp3");

            return false;
        }

    });



//Metodo Compartir
 public void compartir (int a1 ,String a2)
{
    try {

// Abrimos el recurso y lo metemos en un bufer

        InputStream ins = getResources().openRawResource(a1);
        byte[] buffer = new byte[ins.available()];
        ins.read(buffer);
        ins.close();

// Grabamos el bufer en un fichero
        String filename = getExternalCacheDir().toString() + a2;
        FileOutputStream fos = new FileOutputStream(filename);
        fos.write(buffer);
        fos.close();

    } catch (IOException e) {
// TODO Auto-generated catch block
        e.printStackTrace();
    }

    File open = new File(getExternalCacheDir().toString() + a2);

    final Intent comparte = new Intent(Intent.ACTION_SEND);
    comparte.setType("audio/mp3");
    comparte.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(open));
    startActivity(Intent.createChooser(comparte,"Enviar a..."));

        }
    
answered by 22.10.2018 в 02:25