Copying raw file into SDCard?

后端 未结 3 1997
灰色年华
灰色年华 2020-11-28 14:43

I\'ve some audio files in my res/raw folder. For some reasons, i want to copy this files to my SDCard When, my application starts.

How can i done this?

3条回答
  •  有刺的猬
    2020-11-28 15:47

    Copy file from raw to External Storage:

    This is a method that i use to do this job, this method receive the resource id, and the name desired for storage, for example:

    copyFiletoExternalStorage(R.raw.mysound, "jorgesys_sound.mp3");
    

    method:

    private void copyFiletoExternalStorage(int resourceId, String resourceName){
        String pathSDCard = Environment.getExternalStorageDirectory() + "/Android/data/" + resourceName;
        try{
            InputStream in = getResources().openRawResource(resourceId);
            FileOutputStream out = null;
            out = new FileOutputStream(pathSDCard);
            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();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
    }
    

提交回复
热议问题