2

I am making a soundboard app and I need to share sound1 when button1 is long pressed. I can make the share menu com up with this code:

Button button1;


    button1 = (Button) v.findViewById(R.id.button1);
    button1.setLongClickable(true);

    button1.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View arg0) {

            Intent shareIntent = new Intent(Intent.ACTION_SEND);
            shareIntent.setType("audio/ogg");

            Uri.parse("android.resource://test.testapp/" + R.raw.sound1);
            shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://test.testapp/" + R.raw.sound1));
            startActivity(Intent.createChooser(shareIntent, "Share Sound"));

            return true;
        }

    });


    return v;
}

I can share the audio file perfectly with whatsapp and Google Drive, but other apps don't work. I've read that you have to copy the files to the external storage, and share them from there. I have searched for almost two days now, but I can't find a way to do this. Other articles on Stack don't help me either :/

How do I make a directory in the external storage, copy a file (sound1.ogg) from my /raw folder to that folder, and then share that sound with another app (Gmail, Google Drive, Whatsapp, Skype etc.)?

Spickle
  • 47
  • 10
  • Have you see this post? http://stackoverflow.com/questions/17794974/create-folder-in-android –  Nov 02 '15 at 21:38

1 Answers1

0

Saving something to external storage is pretty straightforward. First off you need to add this to your manifest file to allow external storage writing:

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

Then in your activity/fragment:

 String root = Environment.getExternalStorageDirectory().toString();
 File myDir = new File(root + "/saved_images");  
 myDir.mkdirs();  
 File file = new File (myDir, "FILE_NAME");
 ... however you write the file through output stream ....

Since these are audio files, you should store them in the public audio library in each users android phone. You can access it like this:

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)

I would suggest looking through this article for more information!

Shrey
  • 671
  • 7
  • 14
  • Just to add to the answer: Saving these audio files to the public audio folder allows ANY app to be able to access it! So if an app allows posting or uploading audio files of any sort they will be able to access them once you save them in this folder. – Shrey Nov 02 '15 at 21:48
  • I don't think they should be stored in the music folder, as they are really short sounds (1-6 seconds) – Spickle Nov 02 '15 at 21:50