Android copies the files in assets to SD card
Preface
Recently, we received a js file caching task, that is, by intercepting the url of our webView, first load the js file from the file, and then Copy it from the assets if it is not in the file. I think this tool class is very useful, so I'll send it first for your reference. Later, I will write out the whole idea of the project.
Project code
public class CopyAssetsToSd { final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 2, 1, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(100)); private Context mContext; /** * assets Folder js of */ private String assetDir; /** * Destination folder */ private String dir; public CopyAssetsToSd(Context context, String assetDir, String dir) { mContext = context; this.assetDir = assetDir; this.dir = dir; new MyAsyncTask().execute(); } /** * Listening and copying completed */ 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) { //Here's how to pool threads threadPoolExecutor.execute(new Runnable() { @Override public void run() { String[] files; AssetManager assetManager = mContext.getResources().getAssets(); try { // Get the total number of files in the specified folder under the Assets folder files = assetManager.list(assetDir); } catch (IOException e1) { return; } final File mWorkingPath = new File(dir); // If the file path does not exist if (!mWorkingPath.exists()) { mWorkingPath.mkdirs(); } for (int i = 0; i < files.length; i++) { int finalI = i; try { // Get the name of each file String fileName = files[finalI]; File outFile = new File(mWorkingPath + "/" + fileName); // Judge whether the file exists 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(); } } } }); //Here's how threads work // new Thread(new Runnable() { // @Override // public void run() { // String[] files; // AssetManager assetManager = mContext.getResources().getAssets(); // try { // //How many files are there to get Assets // files = assetManager.list(assetDir); // } catch (IOException e1) { // return; // } // final File mWorkingPath = new File(dir); // //If the file path does not exist // if (!mWorkingPath.exists()) { // mWorkingPath.mkdirs(); // } // // for (int i = 0; i < files.length; i++) { // int finalI = i; // // try { // //Get the name of each file // String fileName = files[finalI]; // File outFile = new File(mWorkingPath + "/" + fileName); // //Judge whether the file exists // 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 for operations before asynchronous processing @Override protected void onPreExecute() { super.onPreExecute(); } //The asynchronous task is processed in the doInBackground method @Override protected Bitmap doInBackground(String... params) { copyAssets(assetDir, dir); return null; } //onPostExecute is used for UI update. The parameter of this method is the value returned by doInBackground method @Override protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); if (mOnCopyListener != null) { //Copy completed listening mOnCopyListener.success(); } } } }
Parameter description
parameter | explain |
---|---|
assetDir | A folder under assets (see my project screenshot below) |
dir | Destination folder (to copy the assets file here) |
Project screenshots
Because there are many hidden files under assets, they will be redundant when searching. So we created a folder myjs, so our assetDir parameter is myjs.
epilogue
Because I am busy recently, I have written so much for the time being, and I will make up the project in a period of time.