Android復(fù)制assets文件到SD卡
前言
最近接到一個(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ù)說明
項(xiàng)目截圖:
因?yàn)閍ssets下面有很多隱藏文件,在查找的時(shí)候會(huì)很冗余。所以我們自建了一個(gè)文件夾myjs,所以我們的assetDir參數(shù)是myjs。
結(jié)語
由于最近比較忙,暫時(shí)先寫這么多,項(xiàng)目過一段時(shí)間補(bǔ)上。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android加載Assets目錄中Xml布局文件
- Android實(shí)現(xiàn)復(fù)制Assets文件到SD卡
- 詳解vue2.0 資源文件assets和static的區(qū)別
- iOS開發(fā)之AssetsLibrary框架使用詳解
- Android studio 添加assets文件夾的方法
- 詳解Vue-cli中的靜態(tài)資源管理(src/assets和static/的區(qū)別)
- vue2.0 資源文件assets和static的區(qū)別詳解
- Android開發(fā)實(shí)現(xiàn)讀取Assets下文件及文件寫入存儲(chǔ)卡的方法
- android讀取assets中Excel表格并顯示
- Android獲取其他應(yīng)用中的assets資源
相關(guān)文章
Android實(shí)現(xiàn)用戶登錄記住密碼功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)用戶登錄記住密碼功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Android中ListView下拉刷新的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android中ListView下拉刷新的實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-06-06Android實(shí)現(xiàn)微信聊天語言點(diǎn)擊喇叭動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)微信聊天語言點(diǎn)擊喇叭動(dòng)畫效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Android中毛玻璃效果的兩種實(shí)現(xiàn)代碼
這篇文章主要介紹了Android中毛玻璃效果的兩種實(shí)現(xiàn)代碼,第一種是使用JAVA算法FastBlur實(shí)現(xiàn),第二種是使用Android自帶類RenderScript 實(shí)現(xiàn),本文通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友參考下吧2024-08-08詳解Android使用CoordinatorLayout+AppBarLayout實(shí)現(xiàn)拉伸頂部圖片功能
這篇文章主要介紹了Android使用CoordinatorLayout+AppBarLayout實(shí)現(xiàn)拉伸頂部圖片功能,本文實(shí)例文字相結(jié)合給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10解決Fedora14下eclipse進(jìn)行android開發(fā),ibus提示沒有輸入窗口的方法詳解
本篇文章是對(duì)Fedora14下eclipse進(jìn)行android開發(fā),ibus提示沒有輸入窗口的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05Android總結(jié)之WebView與Javascript交互(互相調(diào)用)
本篇文章主要介紹了WebView與Javascript進(jìn)行數(shù)據(jù)交互,詳解的講訴了WebView與Javascript進(jìn)行數(shù)據(jù)交互的方法,有興趣的可以了解一下。2016-11-11Android自定義控件實(shí)現(xiàn)時(shí)鐘效果
這篇文章主要介紹了Android自定義控件實(shí)現(xiàn)時(shí)鐘效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12