复制Assets文件内容到指定文件夹

下面这段代码估计一般的人看了有点蒙,先简单的解释下,是用来复制Assets文件夹下的一个文件目录到指定的比如"SD"卡上一个指定文件目录下的操作:

package com.handler;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.content.res.AssetManager;

public class DuplicateHandler {


private static DuplicateHandler mDuplicateHandler;
private AssetManager mAssetManager = null;

public static DuplicateHandler getInstance(Client mClient) {
if (mDuplicateHandler == null) {
mDuplicateHandler = new DuplicateHandler(mClient);
}
return mDuplicateHandler;
}

public DuplicateHandler(Client mClient) {
this.mClient = mClient;
}

private String[] getImages(String mDirectory) {
String[] tempImages = null;
try {
mAssetManager = mClient.getAssets();
if (null != mAssetManager) {
tempImages = mAssetManager.list(mDirectory);
}
} catch (IOException e) {
e.printStackTrace();
}
return tempImages;
}

public void initPath(String mDirName) {
try {
//初始化即将保存到的文件夹目录
String mLastPath = Common.mCachePath + "/" + mDirName;
File mLastFolder = new File(mLastPath);
if (!mLastFolder.exists()) {
mLastFolder.mkdirs();
}
mLastFolder = null;
} catch (Exception e) {
e.printStackTrace();
}
}

/** 复制Assets文件夹下的图片文件夹到指定目录. */
public boolean duplicateImages(String mPath) {
initPath(mPath);
String[] mAllImages = getImages(mPath);
InputStream mFileIn = null;
FileOutputStream mFileOut;
File mFile;
for (int i = 0; i < mAllImages.length; i++) {
if (!mAllImages[i].endsWith(".png")
&& !mAllImages[i].endsWith(".jpg")
&& !mAllImages[i].endsWith(".gif")
&& !mAllImages[i].endsWith(".html")
) {
duplicateImages(mPath + "/" + mAllImages[i]);
} else {
try {
mFile = new File(Common.mCachePath + "/" + mPath + "/"
+ mAllImages[i]);
if (!mFile.exists()) {
mFile.createNewFile();
}
mFileIn = mAssetManager.open(mPath + "/" + mAllImages[i]);
mFileOut = new FileOutputStream(mFile);
int readedBytes;
byte[] buf = new byte[128];
while ((readedBytes = mFileIn.read(buf)) > 0) {
mFileOut.write(buf, 0, readedBytes);
}
mFileOut.flush();
mFileOut.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
return true;

// BitmapDrawable bitmapDrawable = (BitmapDrawable) image.getDrawable();
// if (bitmapDrawable != null &&
// !bitmapDrawable.getBitmap().isRecycled()) {
// bitmapDrawable.getBitmap().recycle();
// }
// image.setImageBitmap(BitmapFactory.decodeStream(assetFile));
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值