Assets 파일 내용을 지정된 폴더로 복사

3335 단어 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));
	}
}



좋은 웹페이지 즐겨찾기