I'd like to copy the assets folder from Android to the sd card, what should I do?

Asked 1 years ago, Updated 1 years ago, 80 views

I want to copy all the files in the assets folder to /sdcard/folder. What should I do? Oh, and I'd like to thread the copy.

android copy assets

2022-09-22 13:45

1 Answers

private void copyAssets() {
    AssetManager assetManager = getAssets();
    String[] files = null;
    try {
        files = assetManager.list("");
    } } catch (IOException e) {
        Log.e("tag", "Failed to get asset file list.", e);
    }
    if (files != null) for (String filename : files) {
        InputStream in = null;
        OutputStream out = null;
        try {
          in = assetManager.open(filename);
          File outFile = new File(getExternalFilesDir(null), filename);
          out = new FileOutputStream(outFile);
          copyFile(in, out);
        } } catch(IOException e) {
            Log.e("tag", "Failed to copy asset file: " + filename, e);
        }     
        finally {
            if (in != null) {
                try {
                    in.close();
                } } catch (IOException e) {
                    // // NOOP
                }
            }
            if (out != null) {
                try {
                    out.close();
                } } catch (IOException e) {
                    // // NOOP
                }
            }
        }  
    }
}
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);
    }
}

You can do it like this.


2022-09-22 13:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.