首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

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

2012-06-26 
复制Assets文件内容到指定文件夹下面这段代码估计一般的人看了有点蒙,先简单的解释下,是用来复制Assets文

复制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));}}

热点排行