Android開發(2)--Android資源訪問機制
阿新 • • 發佈:2018-12-28
在開發中需要引用程式資源,比如專案中assets和res目錄下的圖片、layout、values等或者需要系統內建的資源。
資源分為兩種:
第一種:res目錄下的資源(不會被編譯,但是會生成id)
第二種:assets資料夾下的資原始檔,又叫原始資原始檔(不會被編譯,也不會生成id)
1、建立Assets資料夾
右鍵目標資料夾進行建立
2、獲取Assets資料夾的管理類
AssetManager assets = getAssets();
3、遍歷資料夾下的資源列表
String[] list = assets.list();
在app/src/main/java/lwplw.com.helloworld/FileUtils.java
String fileNames[] = context.getAssets().list(srcPath);
這句就相當於一次實現了上面的2和3。
4、獲取Assets資料夾的資源
5、示例程式
package lwplw.com.helloworld; import android.content.Context; import android.os.Environment; import android.os.Handler; import android.os.Looper; import android.os.Message; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; public class FileUtils { private static FileUtils instance; private static final int SUCCESS = 1; private static final int FAILED = 0; private Context context; private FileOperateCallback callback; private volatile boolean isSuccess; private String errorStr; public static FileUtils getInstance(Context context) { if (instance == null) instance = new FileUtils(context); return instance; } private FileUtils(Context context) { this.context = context; } private Handler handler = new Handler(Looper.getMainLooper()) { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (callback != null) { if (msg.what == SUCCESS) { callback.onSuccess(); } if (msg.what == FAILED) { callback.onFailed(msg.obj.toString()); } } } }; public FileUtils copyAssetsToSD(final String srcPath, final String sdPath) { new Thread(new Runnable() { @Override public void run() { copyAssetsToDst(context, srcPath, sdPath); if (isSuccess) handler.obtainMessage(SUCCESS).sendToTarget(); else handler.obtainMessage(FAILED, errorStr).sendToTarget(); } }).start(); return this; } public void setFileOperateCallback(FileOperateCallback callback) { this.callback = callback; } private void copyAssetsToDst(Context context, String srcPath, String dstPath) { try { String fileNames[] = context.getAssets().list(srcPath); if (fileNames.length > 0) { File file = new File(Environment.getExternalStorageDirectory(), dstPath); if (!file.exists()) file.mkdirs(); for (String fileName : fileNames) { if (!srcPath.equals("")) { // assets 資料夾下的目錄 copyAssetsToDst(context, srcPath + File.separator + fileName, dstPath + File.separator + fileName); } else { // assets 資料夾 copyAssetsToDst(context, fileName, dstPath + File.separator + fileName); } } } else { File outFile = new File(Environment.getExternalStorageDirectory(), dstPath); InputStream is = context.getAssets().open(srcPath); FileOutputStream fos = new FileOutputStream(outFile); byte[] buffer = new byte[1024]; int byteCount; while ((byteCount = is.read(buffer)) != -1) { fos.write(buffer, 0, byteCount); } fos.flush(); is.close(); fos.close(); } isSuccess = true; } catch (Exception e) { e.printStackTrace(); errorStr = e.getMessage(); isSuccess = false; } } public interface FileOperateCallback { void onSuccess(); void onFailed(String error); } }
6、使用舉例
見app/src/main/java/lwplw.com.helloworld/MainActivity.java
檔案
函式功能:
(1)在手機儲存卡建立目錄結構lwplw_helloworld/data
(2)把assets資料夾中的資源,全部copy倒sd卡lwplw_helloworld/data目錄
7、注意事項
(1)assets檔案是壓縮在apk中的,apk執行時無法直接訪問該目錄,需要copy出來。
(2)需要等到assets檔案中資源全部copy完成後,回撥到 onSuccess(),再執行後續操作。