Copying directories and files from res/raw folder to sd card - android
Asked Answered
B

1

6

I have a folder which contains few files and some directories which I need to copy to my SD card's /mnt/sdcard/Android/data/ path while I launch the application for the first time, and of course, if not already the required folder is not present in that path.

I will have this folder inside res/raw folder of my application.

What are the step by step procedures I need to do such that I can copy the folder and all its contents from res/raw to the specified path in the SD card.

Any help is much appreciated.

Edit

The following is the solution if it helps someone else:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    copyFileOrDir("edu1");//directory name in assets
}
File sdCard = Environment.getExternalStorageDirectory();
private void copyFileOrDir(String path) {
    AssetManager assetManager = this.getAssets();
    String assets[] = null;
    try {
        assets = assetManager.list(path);
        if (assets.length == 0) {
            copyFile(path);
        } else {
            File dir = new File (sdCard.getAbsolutePath() + "/" + "Android/data");
            //String fullPath = "/data/data/" + this.getPackageName() + "/" + path;//path for storing internally to data/data
            //File dir = new File(fullPath);
            if (!dir.exists()){
                System.out.println("Created directory"+sdCard.getAbsolutePath() + "/Android/data");
                boolean result = dir.mkdir();
                System.out.println("Result of directory creation"+result);
            }

            for (int i = 0; i < assets.length; ++i) {
                copyFileOrDir(path + "/" + assets[i]);
            }
        }
    } catch (IOException ex) {
        System.out.println("Exception in copyFileOrDir"+ex);
    }
}

private void copyFile(String filename) {
    AssetManager assetManager = this.getAssets();

    InputStream in = null;
    OutputStream out = null;
    try {
        in = assetManager.open(filename);
        //String newFileName = "/data/data/" + this.getPackageName() + "/" + filename;//path for storing internally to data/data
        String newFileName = sdCard.getAbsolutePath() + "/Android/data/" + filename;
        out = new FileOutputStream(newFileName);
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    } catch (Exception e) {
        System.out.println("Exception in copyFile"+e);
    }

}
 }
Badge answered 18/6, 2012 at 13:55 Comment(2)
You mind posting some code on what you've done so far? It looks like you're just saying "Here's what I want to do, now code it for me."Chiliarch
not really, I am editing my question.Badge
M
3

I suggest you the keep your files in assets. The following code may help you to copy contents from assets directory to SD Card.

public static void copyFile(Activity c, String filename) 
{
    AssetManager assetManager = c.getAssets();

    InputStream in = null;
    OutputStream out = null;
    try 
    {
        in = assetManager.open(filename);
        String newFileName = sdcardpath/filename;
        out = new FileOutputStream(newFileName);

        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) 
        {
            out.write(buffer, 0, read);
        }
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    } catch (Exception e) {
        Utility.printLog("tag", e.getMessage());
    }finally{
        if(in!=null){
            try {
                in.close();
            } catch (IOException e) {
                printLog(TAG, "Exception while closing input stream",e);
            }
        }
        if(out!=null){
            try {
                out.close();
            } catch (IOException e) {
                printLog(TAG, "Exception while closing output stream",e);
            }
        }
    }
}
Mocambique answered 18/6, 2012 at 14:19 Comment(4)
As I mentioned in my qstion clearly, it is not files alone that I need to copy, but folders and sub folders alsoBadge
This link will help you to copy resource from raw resource #939670Mocambique
If you need to copy sub-folders too from assets , you need to modify the copy little bit.Mocambique
ok, I am pasting the solution I used as edit to my question if in case it helps someone in future.Badge

© 2022 - 2024 — McMap. All rights reserved.