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-permission android: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;

public class Sample extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final int[] mSongs = new int[] { R.raw.background, R.raw.background1, R.raw.background2 };
    for (int i = 0; i < mSongs.length; i++) {
        try {
            String path = Environment.getExternalStorageDirectory() + "/PriyankaChopra";
            File dir = new File(path);
            if (dir.mkdirs() || dir.isDirectory()) {
                String str_song_name = i + ".mp3";
                CopyRAWtoSDCard(mSongs[i], path + File.separator + str_song_name);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

private void CopyRAWtoSDCard(int id, String path) throws IOException {
    InputStream in = getResources().openRawResource(id);
    FileOutputStream out = new FileOutputStream(path);
    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();
    }
}

}

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"