Created
June 17, 2014 07:50
-
-
Save thinzaroo/5aef6e81638529a89995 to your computer and use it in GitHub Desktop.
Copy files from Android's assets folder to external storage (We will put some defaults files in /Assets folder, copy to /data/data/PACKAGE_NAME/images folder at runtime. And later, if new files available, the app will download images and replace to data/data folder)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void copyAssets() { | |
AssetManager assetManager = getAssets(); | |
String[] files = null; | |
try { | |
files = assetManager.list("img"); | |
} catch (IOException e) { | |
Log.e("tag", "Failed to get asset file list.", e); | |
} | |
for(String filename : files) { | |
InputStream in = null; | |
OutputStream out = null; | |
try { | |
in = assetManager.open("img/" + filename); | |
File outFile = new File(sdCardPath +"/", filename); | |
out = new FileOutputStream(outFile); | |
copyFile(in, out); | |
in.close(); | |
in = null; | |
out.flush(); | |
out.close(); | |
out = null; | |
} catch(IOException e) { | |
Log.e("tag", "Failed to copy asset file: " + filename, e); | |
} | |
} | |
} | |
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment