How to Copy Raw files into SD Card

前端 未结 2 1740
忘了有多久
忘了有多久 2020-12-11 05:01

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...

<
相关标签:
2条回答
  • 2020-12-11 05:06

    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

    0 讨论(0)
  • 2020-12-11 05:28

    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();
        }
    }
    
    }
    
    0 讨论(0)
提交回复
热议问题