Skip to content Skip to sidebar Skip to footer

How To Copy Raw Files Into Sd Card

I am having problem, while trying to copy audio files from raw folder to SD Card, I have successfully created folder in SD Card, but not able to copy songs in that... I am using th

Solution 1:

Replace your code with mine.

it is working perfect here.

Add only this Permission :

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

Java Code :

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;

publicclassSampleextendsActivity {

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    finalint[] mSongs = newint[] { R.raw.background, R.raw.background1, R.raw.background2 };
    for (inti=0; i < mSongs.length; i++) {
        try {
            Stringpath= Environment.getExternalStorageDirectory() + "/PriyankaChopra";
            Filedir=newFile(path);
            if (dir.mkdirs() || dir.isDirectory()) {
                Stringstr_song_name= i + ".mp3";
                CopyRAWtoSDCard(mSongs[i], path + File.separator + str_song_name);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

privatevoidCopyRAWtoSDCard(int id, String path)throws IOException {
    InputStreamin= getResources().openRawResource(id);
    FileOutputStreamout=newFileOutputStream(path);
    byte[] buff = newbyte[1024];
    intread=0;
    try {
        while ((read = in.read(buff)) > 0) {
            out.write(buff, 0, read);
        }
    } finally {
        in.close();
        out.close();
    }
}

}

Solution 2:

you forget to add song file name and extension with path on sdcard :

CopyRAWtoSDCard(mSongs[i], "/sdcard/PriyankaChopra/");  <<<<<

do it as :

String str_song_name="song_name_"+i+".mp3";
 CopyRAWtoSDCard(mSongs[i], "/sdcard/PriyankaChopra/"+str_song_name);

and also use Environment.getExternalStorageDirectory() for getting sdcard path instead of static path

Post a Comment for "How To Copy Raw Files Into Sd Card"