Android Tutorial

12.27.2011

Copy file from assets to app in android



/**********************************
*
* Copy List file from folder if fileName != null : copy file of folderName
* if folderName == null: copy file of folder root assets else if folderName
* == null : copy all file of folder root assets
*
*
* *********************************/
private void copyFileFromAssetsToApp(String folderName, String fileName,
String folderNameInApp, String fileNameInApp, Context Ctx) {
Log.d(TAG, "copyClipart start");
AssetManager assetManager = getResources().getAssets();
final String root_dir = "/data/data/" + Ctx.getPackageName();
String basepath = root_dir + "/" + folderNameInApp;
if (Utils.checkNullOrEmpty(folderNameInApp)) {
basepath = root_dir;
}
File clipartdir = new File(basepath);
if (!clipartdir.exists()) {
clipartdir.mkdirs();
}



if (!Utils.checkNullOrEmpty(fileName)) {// Copy file
Log.d(TAG, "Copy file");
InputStream in = null;
OutputStream out = null;
try {
String pathIn = folderName + "/" + fileName;
if (Utils.checkNullOrEmpty(folderName)) {
pathIn = fileName;
}
String pathOut = basepath + "/" + fileNameInApp;
in = assetManager.open(pathIn);
out = new FileOutputStream(pathOut);
copy(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e) {
Log.e("copy clipart ERROR", e.toString());
e.printStackTrace();
}
} else {// Copy All file of folder
Log.d(TAG, "Copy folder");
String[] files = null;
String folderNameFull = folderName + "/";
try {
// folder == null, getList folder root
if (folderName.equals("") || folderName == null
|| folderName.length() == 0) {
files = assetManager.list("");
folderNameFull = "";
} else {
files = assetManager.list(folderName);
}
} catch (Exception e) {
Log.e("read clipart ERROR", e.toString());
e.printStackTrace();
}
for (int i = 0; i < files.length; i++) {
Log.d(TAG, "files[i] = " + files[i]);
InputStream in = null;
OutputStream out = null;
try {
String pathIn = folderNameFull + files[i];
String pathOut = basepath + "/" + files[i];
in = assetManager.open(pathIn);
out = new FileOutputStream(pathOut);
copy(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e) {
Log.e("copy clipart ERROR", e.toString());
e.printStackTrace();
}
}
}
Log.d(TAG, "copyClipart end");
}


/* ------------- File Processes -------------> */
/**
 * Copy data from $from to $to
 *
 * @param $from
 *            source
 * @param $to
 *            destination
 * @throws IOException
 */
public static void copy(InputStream $from, OutputStream $to)
throws IOException {
byte[] buffer = new byte[1024];
int length;
while ((length = $from.read(buffer)) > 0) {
$to.write(buffer, 0, length);
}
$to.flush();
$to.close();
$from.close();
}


1 comment:

  1. Anonymous4/08/2014

    Thanks for posting this. I want to copy a file from assets dir to my app dir, and then execute it there. I hope that is a reasonable thing to do. One question - what package does "Utils" come from?

    ReplyDelete