Android复制assets文件到SD卡

2023-04-27,,

前言

最近接到一个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。

结语

由于最近比较忙,暂时先写这么多,项目过一段时间补上。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。

《Android复制assets文件到SD卡.doc》

下载本文的Word格式文档,以方便收藏与打印。