1. 程式人生 > >Android複製assets檔案到SD卡

Android複製assets檔案到SD卡

Android 複製assets裡面的檔案到SD卡

前言

最近接到一個js檔案快取任務,即通過攔截我們webView的url,首先從檔案載入js檔案,檔案裡沒有的話就去assets裡面Copy過來。感覺這個工具類挺有用的,所以先發上來供大家參考。稍後有時間會把整個專案思路寫出來。

專案程式碼

public class CopyAssetsToSd {
    final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 2, 1, TimeUnit.SECONDS,
            new LinkedBlockingQueue
<Runnable>(100)); private Context mContext; /** * assets的資料夾 js */ private String assetDir; /** * 目標資料夾 */ private String dir; public CopyAssetsToSd(Context context, String assetDir, String dir) { mContext = context; this.assetDir = assetDir;
this.dir = dir; new MyAsyncTask().execute(); } /** * 監聽複製完成 */ public interface onCopyListener { void success(); } private onCopyListener mOnCopyListener; public void setOnCopyListener(onCopyListener onCopyListener) { this.mOnCopyListener =
onCopyListener; } public void copyAssets(final String assetDir, final String dir) { //下面是執行緒池的方法 threadPoolExecutor.execute(new Runnable() { @Override public void run() { String[] files; AssetManager assetManager = mContext.getResources().getAssets(); try { // 獲得Assets資料夾下指定資料夾一共有多少檔案 files = assetManager.list(assetDir); } catch (IOException e1) { return; } final File mWorkingPath = new File(dir); // 如果檔案路徑不存在 if (!mWorkingPath.exists()) { mWorkingPath.mkdirs(); } for (int i = 0; i < files.length; i++) { int finalI = i; try { // 獲得每個檔案的名字 String fileName = files[finalI]; File outFile = new File(mWorkingPath + "/" + fileName); // 判斷檔案是否存在 if (!outFile.exists()) { outFile.createNewFile(); FileOutputStream out = new FileOutputStream(outFile); InputStream in = null; if (0 != assetDir.length()) in = assetManager.open(assetDir + "/" + fileName); else in = assetManager.open(fileName); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); } else { } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } }); //下面是執行緒的方法 // new Thread(new Runnable() { // @Override // public void run() { // String[] files; // AssetManager assetManager = mContext.getResources().getAssets(); // try { // // 獲得Assets一共有幾多檔案 // files = assetManager.list(assetDir); // } catch (IOException e1) { // return; // } // final File mWorkingPath = new File(dir); // // 如果檔案路徑不存在 // if (!mWorkingPath.exists()) { // mWorkingPath.mkdirs(); // } // // for (int i = 0; i < files.length; i++) { // int finalI = i; // // try { // // 獲得每個檔案的名字 // String fileName = files[finalI]; // File outFile = new File(mWorkingPath + "/" + fileName); // // 判斷檔案是否存在 // if (!outFile.exists()) { // outFile.createNewFile(); // FileOutputStream out = new FileOutputStream(outFile); // InputStream in = null; // if (0 != assetDir.length()) // in = assetManager.open(assetDir + "/" + fileName); // else // in = assetManager.open(fileName); // // Transfer bytes from in to out // byte[] buf = new byte[1024]; // int len; // while ((len = in.read(buf)) > 0) { // out.write(buf, 0, len); // } // in.close(); // out.close(); // } else { // // // } // } catch (FileNotFoundException e) { // e.printStackTrace(); // } catch (IOException e) { // e.printStackTrace(); // } // } // // } // // }).start(); } class MyAsyncTask extends AsyncTask<String, Void, Bitmap> { //onPreExecute用於非同步處理前的操作 @Override protected void onPreExecute() { super.onPreExecute(); } //在doInBackground方法中進行非同步任務的處理. @Override protected Bitmap doInBackground(String... params) { copyAssets(assetDir, dir); return null; } //onPostExecute用於UI的更新.此方法的引數為doInBackground方法返回的值. @Override protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); if (mOnCopyListener != null) { //複製完成的監聽 mOnCopyListener.success(); } } } }

引數說明

引數 解釋
assetDir assets下面的某個資料夾(請看下面我的專案截圖)
dir 目標資料夾(要把assets檔案複製到這裡)

專案截圖 專案截圖 因為assets下面有很多隱藏檔案,在查詢的時候會很冗餘。所以我們自建了一個資料夾myjs,所以我們的assetDir引數是myjs

結語

由於最近比較忙,暫時先寫這麼多,專案過一段時間補上。