Android Studio problems when sharing mp3

2

This is my code:

peroperoperopero.setOnLongClickListener(new View.OnLongClickListener() {
@Override
        public boolean onLongClick(View v) {
            Intent compartirAudio = new Intent(android.content.Intent.ACTION_SEND);
            compartirAudio.setType("com.whatsapp");
            compartirAudio.setType("audio/mp3");
            compartirAudio.putExtra(Intent.EXTRA_STREAM,
                    Uri.parse("android.resource://com.example.lucas.botoneradeteloresumoasinomas/" + R.raw.peroperopero));
            startActivity(Intent.createChooser(compartirAudio, "Compartir vía"));
            return false;
        }
    });

I keep pressing the button I get the menu to share, I share it in a WhatsApp group and send a file of document type that weighs 50kb and can not be opened with anything, that I have to change or add to work ? thank you very much.

    
asked by Lucas Fajersztejn 05.10.2018 в 07:32
source

2 answers

0
peroperoperopero.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Intent compartirAudio = new Intent(android.content.Intent.ACTION_SEND);
            compartirAudio.setType("com.whatsapp");
            compartirAudio.setType("audio/mp3");
            compartirAudio.putExtra(Intent.EXTRA_STREAM,
                    Uri.parse("android.resource://com.example.lucas.botoneradeteloresumoasinomas/" + R.raw.peroperopero));
            startActivity(Intent.createChooser(compartirAudio, "Compartir vía"));
            File myFile = new File("res/raw/peroperopero");
            Uri newUri = FileProvider.getUriForFile(getApplicationContext(), "com.example.myapp.fileprovider", myFile);
            return false;
    
answered by 22.10.2018 в 00:45
0

Hello your problem is that the audio file is not accessible by whatsapp since it belongs to your application. What you should do is use a FileProvider that just serves to share files between applications.

In the manifest you should put this inside the tag application

<application>
   <provider
      android:name="android.support.v4.content.FileProvider"
      android:authorities="com.example.lucas.botoneradeteloresumoasinomas.fileprovider"
      android:grantUriPermissions="true"
      android:exported="false">
      <meta-data
         android:name="android.support.FILE_PROVIDER_PATHS"
         android:resource="@xml/filepaths" />
   </provider>
</application>

In you should create the file filepaths.xml in the directory res/xml/ in you will have to put something like this:

<paths>
   <files-path path="music/" name="mymusic"/>
</paths>

Finally to get the URI you should only use the getUriForFile method provided by FileProvider and use that URI to share the file.

File myFile = new File("res/raw/peroperopero");
Uri newUri = getUriForFile(getContext(), "com.example.myapp.fileprovider", myFile);

With this uri you should be able to share the file as you were trying

File myFile = new File("res/raw/peroperopero");
Uri newUri = getUriForFile(getContext(), "com.example.lucas.botoneradeteloresumoasinomas.fileprovider", myFile);
Intent compartirAudio = new Intent(android.content.Intent.ACTION_SEND);
compartirAudio.setType("com.whatsapp");
compartirAudio.setType("audio/mp3");
compartirAudio.putExtra(Intent.EXTRA_STREAM,newUri);
startActivity(Intent.createChooser(compartirAudio, "Compartir vía"));

I leave a link with a more detailed explanation of how to share files using file provider in addition to the FileProvider documentation

    
answered by 05.10.2018 в 17:14