Android Tutorial

12.27.2011

Copy and delete file in android



private void copyFile(String pathIn, String pathOut) {
Log.d(TAG, "copyFile start");
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(pathIn);
out = new FileOutputStream(pathOut);
copy(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
delFile(pathIn);
} catch (Exception e) {
Log.e("copy clipart ERROR", e.toString());
e.printStackTrace();
}
Log.d(TAG, "copyFile end");
}

private boolean delFile(String filePath) {
File file = new File(filePath);
boolean deleted = file.delete();
return deleted;
}

/* ------------- 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();
}

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();
}

Unzip file not password in Android



/****************************************************************************
* Unzip file not password
*
* Example:
* String path = "/sdcard/files.zip";
* String folderOut = "/data/data" + mCtx.getPackageName() + "/folder";
* File fi = new File(path);
* InputStream zipIn = new FileInputStream(fi);
* // if asset : AssetManager assetManager = getResources().getAssets();
* // InputStream zipIn = assetManager.open("files.zip");
* unZipFileNotPass(zipIn, folderOut);
*
******************************************************************************/