Skip to content

Instantly share code, notes, and snippets.

@mistrydarshan99
Forked from vaibhav-jani/FileUtils.Java
Last active August 28, 2015 10:50
Show Gist options
  • Save mistrydarshan99/5cd0b70f5fbe5bdc1408 to your computer and use it in GitHub Desktop.
Save mistrydarshan99/5cd0b70f5fbe5bdc1408 to your computer and use it in GitHub Desktop.
import android.content.Context;
import android.content.res.AssetManager;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Created by vaibhav.jani on 5/25/2015.
*/
public class FileUtils {
public static void copyFileOrDirFromAsset(String path, Context context) {
AssetManager assetManager = context.getAssets();
String assets[] = null;
try {
assets = assetManager.list(path);
if (assets.length == 0) {
copyFileFromAssets(path, context);
} else {
String fullPath = Environment.getExternalStorageDirectory() + "/" + path;
File dir = new File(fullPath);
if (!dir.exists()){
dir.mkdir();
}
for (int i = 0; i < assets.length; ++i) {
copyFileOrDirFromAsset(path + "/" + assets[i], context);
}
}
} catch (IOException ex) {
Log.e("tag", "I/O Exception", ex);
}
}
public static void copyFileFromAssets(String filename, Context context) {
AssetManager assetManager = context.getAssets();
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
String newFileName = Environment.getExternalStorageDirectory() + "/" + 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());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment