1

I have a soundboard app and am trying to enable the user to share the sounds from my res/raw folder. My code seems to properly create a folder and copy the resource into it. However, when trying to share, I always get an error from the external app. (e.g. gmail says: "Cannot attach file"). Can anyone please help?

The code:

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Pls {
    public void sendAudio(Activity activity){
        try {
            String mediaPath = copyFiletoExternalStorage(R.raw.audio_file, "audiofile.mp3",activity);
            Intent shareMedia = new Intent(Intent.ACTION_SEND);
            shareMedia.setType("audio/*");
            shareMedia.putExtra(Intent.EXTRA_STREAM, Uri.parse(mediaPath));
            activity.startActivity(Intent.createChooser(shareMedia, "Select app"));
        } catch (Exception e) {
            Toast.makeText(activity.getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
        }
    }

    private String copyFiletoExternalStorage(int resourceId, String resourceName,Activity activity){
        String pathSDCard = Environment.getExternalStorageDirectory() + "/Android/data/" + resourceName;
        try{
            InputStream in = activity.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 (IOException e) {
            Log.i("err",e.toString());
        }
        return  pathSDCard;
    }
}
Kanzariya Hitesh
  • 118
  • 1
  • 15
  • try solution provided in this https://stackoverflow.com/questions/35959773/share-audio-file-from-the-res-raw-folder-thorugh-share-intent-in-android – Amod Gokhale Feb 22 '20 at 17:08

0 Answers0