国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android復制assets文件到SD卡

2019-10-21 21:32:14
字體:
來源:轉載
供稿:網友

前言

最近接到一個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();      }    }  }}

參數說明

Android,復制,assets,SD卡

項目截圖:

Android,復制,assets,SD卡

因為assets下面有很多隱藏文件,在查找的時候會很冗余。所以我們自建了一個文件夾myjs,所以我們的assetDir參數是myjs。

結語

由于最近比較忙,暫時先寫這么多,項目過一段時間補上。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 彭泽县| 昔阳县| 左云县| 赞皇县| 临潭县| 涪陵区| 武平县| 宁强县| 鞍山市| 湖北省| 高安市| 芒康县| 焉耆| 嘉定区| 卫辉市| 威海市| 达日县| 扶风县| 开江县| 乐业县| 光泽县| 保亭| 顺义区| 玛纳斯县| 旺苍县| 万荣县| 驻马店市| 彭山县| 台江县| 桐柏县| 怀集县| 若尔盖县| 遵义县| 井研县| 乾安县| 沙雅县| 肥西县| 拉萨市| 长白| 常宁市| 大渡口区|