当前位置: 移动技术网 > IT编程>移动开发>Android > Android复制assets文件到SD卡

Android复制assets文件到SD卡

2019年07月30日  | 移动技术网IT编程  | 我要评论

前言

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

结语

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

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网