服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android复制assets文件到SD卡

Android复制assets文件到SD卡

2022-09-05 15:34tangxuesong6 Android

这篇文章主要为大家详细介绍了Android复制assets文件到SD卡的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

前言

最近接到一个js文件缓存任务,即通过拦截我们webView的url,首先从文件加载js文件,文件里没有的话就去assets里面Copy过来。感觉这个工具类挺有用的,所以先发上来供大家参考。稍后有时间会把整个项目思路写出来。

项目代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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。

结语

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

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

原文链接:https://blog.csdn.net/songtangxue/article/details/83830831

延伸 · 阅读

精彩推荐