Android復(fù)制assets文件到SD卡

前言

創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、外貿(mào)營銷網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的昌邑網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

最近接到一個(gè)js文件緩存任務(wù),即通過攔截我們webView的url,首先從文件加載js文件,文件里沒有的話就去assets里面Copy過來。感覺這個(gè)工具類挺有用的,所以先發(fā)上來供大家參考。稍后有時(shí)間會(huì)把整個(gè)項(xiàng)目思路寫出來。

項(xiàng)目代碼

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;
  /**
   * 目標(biāo)文件夾
   */
  private String dir;

  public CopyAssetsToSd(Context context, String assetDir, String dir) {
    mContext = context;
    this.assetDir = assetDir;
    this.dir = dir;
    new MyAsyncTask().execute();

  }

  /**
   * 監(jiān)聽復(fù)制完成
   */
  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 {
            // 獲得每個(gè)文件的名字
            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 {
//            // 獲得每個(gè)文件的名字
//            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方法中進(jìn)行異步任務(wù)的處理.
    @Override
    protected Bitmap doInBackground(String... params) {
      copyAssets(assetDir, dir);
      return null;
    }

    //onPostExecute用于UI的更新.此方法的參數(shù)為doInBackground方法返回的值.
    @Override
    protected void onPostExecute(Bitmap bitmap) {
      super.onPostExecute(bitmap);
      if (mOnCopyListener != null) {
      //復(fù)制完成的監(jiān)聽
        mOnCopyListener.success();
      }
    }
  }
}

參數(shù)說明

Android復(fù)制assets文件到SD卡

項(xiàng)目截圖:

Android復(fù)制assets文件到SD卡

因?yàn)閍ssets下面有很多隱藏文件,在查找的時(shí)候會(huì)很冗余。所以我們自建了一個(gè)文件夾myjs,所以我們的assetDir參數(shù)是myjs。

結(jié)語

由于最近比較忙,暫時(shí)先寫這么多,項(xiàng)目過一段時(shí)間補(bǔ)上。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

本文標(biāo)題:Android復(fù)制assets文件到SD卡
標(biāo)題URL:http://muchs.cn/article38/jcpjsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、搜索引擎優(yōu)化、網(wǎng)站建設(shè)網(wǎng)站收錄、網(wǎng)頁設(shè)計(jì)公司、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司