Goofy
Goofy

Reputation: 6128

Downloading an audio file from raw folder to SD card

I need to download a file from an application. I have 5 audio files in a raw folder. On the onclick event of a button I need to select one audio file from 5 files and download it to an SD card. How can I acheive this?

Upvotes: 2

Views: 1203

Answers (1)

ConductedClever
ConductedClever

Reputation: 4305

this is so simple but mistakefull ... try this code :

            File directoryTest = new File(
                Environment.getExternalStorageDirectory(), "raw2sd");
            try {
                //coping sound file to sd
                //defining specific directory
                File soundDir = new File(directoryTest, "ORG");
                //making directories
                soundDir.mkdirs();
                FileOutputStream sound = new FileOutputStream(
                        soundDir.getPath() + "/soundName.mp3");
                InputStream is = getResources().openRawResource(R.raw.soundFile);
                int a = is.available();
                byte[] buf = new byte[a];
                is.read(buf, 0, a);
                sound.write(buf);
                sound.flush();
                sound.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            }

this is 100% tested.

Upvotes: 1

Related Questions