8

I am trying to create a folder and several subdirectory within it on the SD Card... I then want to transfer files that I have stored in /res/raw to that folder... I addition, I want this to only happen once, the first time the program is ever run. I realize that this is ridiculously open-ended, and that I am asking a lot... but any help would be greatly appreciated.

Frank Bozzo
  • 313
  • 1
  • 4
  • 8

2 Answers2

9

This will copy all files in the "clipart" subfolder of the .apk assets folder to the "clipart" subfolder of your app's folder on the SD card:

String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    String basepath = extStorageDirectory + "/name of your app folder on the SD card";
//...

// in onCreate
File clipartdir = new File(basepath + "/clipart/");
        if (!clipartdir.exists()) {
            clipartdir.mkdirs();
            copyClipart();      
        }

private void copyClipart() {
        AssetManager assetManager = getResources().getAssets();
        String[] files = null;
        try {
            files = assetManager.list("clipart");
        } catch (Exception e) {
            Log.e("read clipart ERROR", e.toString());
            e.printStackTrace();
        }
        for(int i=0; i<files.length; i++) {
            InputStream in = null;
            OutputStream out = null;
            try {
              in = assetManager.open("clipart/" + files[i]);
              out = new FileOutputStream(basepath + "/clipart/" + files[i]);
              copyFile(in, out);
              in.close();
              in = null;
              out.flush();
              out.close();
              out = null;
            } catch(Exception e) {
                Log.e("copy clipart ERROR", e.toString());
                e.printStackTrace();
            }       
        }
    }
    private void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while((read = in.read(buffer)) != -1){
          out.write(buffer, 0, read);
        }
    }
Giulio Prisco
  • 941
  • 9
  • 16
  • where does this code need to be to make sure it only happens once, on install, and then remove the files from the APK? – joon May 19 '13 at 15:59
  • @joon: You can use [link](http://developer.android.com/reference/android/content/SharedPreferences.html) to save a boolean value to make sure it only happens once. By the way, you can't delete files from apk.@see more: [link](http://developer.android.com/guide/topics/resources/providing-resources.html) – Justin Mar 03 '14 at 16:05
0

I experienced a similar problem when using mkdirs(), however because running the command:

mkdir one/two

fails on Linux, then the method http://download.oracle.com/javase/1.4.2/docs/api/java/io/File.html#mkdirs() subsequently fails too. I guess this means there is no way to use mkdirs on Android? My (probably rather hacky) work-around was to create each necessary directory separately:

String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
new File(extStorageDirectory + "/one/").mkdirs();
new File(extStorageDirectory + "/one/two/).mkdirs();
user3833732
  • 886
  • 9
  • 14
Fofx
  • 141
  • 1
  • 1
  • 3
  • [File.mkDirs()](http://developer.android.com/reference/java/io/File.html#mkdirs%28%29) works. I've tried it on Android 2.1 (API Level 7) – Mudassir Jul 19 '11 at 12:17