Skip to content

Instantly share code, notes, and snippets.

@tylerchesley
Created August 9, 2013 23:10
Show Gist options
  • Save tylerchesley/6198074 to your computer and use it in GitHub Desktop.
Save tylerchesley/6198074 to your computer and use it in GitHub Desktop.
Function to recursively copy files from an Android asset directory
public void copyFileOrDir(String path) {
AssetManager assetManager = this.getAssets();
String assets[] = null;
try {
assets = assetManager.list(path);
if (assets.length == 0) {
copyFile(path);
} else {
String fullPath = "/data/data/" + this.getPackageName() + "/" + path;
File dir = new File(fullPath);
if (!dir.exists())
dir.mkdir();
for (int i = 0; i < assets.length; ++i) {
copyFileOrDir(path + "/" + assets[i]);
}
}
} catch (IOException ex) {
Log.e("tag", "I/O Exception", 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;
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) {
Log.e("tag", e.getMessage());
}
}
@Pycia
Copy link

Pycia commented Sep 10, 2014

That's work perfect! Thanks!

@polyakoff
Copy link

Does it work as expected with file names starting with dot?
Thanks in advance.

@22224444
Copy link

Thank you

@essare
Copy link

essare commented Jun 20, 2016

Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment