前言
最近接到一個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(); } } }}
參數說明
項目截圖:
因為assets下面有很多隱藏文件,在查找的時候會很冗余。所以我們自建了一個文件夾myjs,所以我們的assetDir參數是myjs。
結語
由于最近比較忙,暫時先寫這么多,項目過一段時間補上。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答