Android 資源-raw和assets

前言

這幾天遇到了一個坑!關于resource相關的操作。具體的需求是:替換所有班級的logo,但是移動端android和ios沒有建立協議,所以臨時采用的是直接上傳圖片來完成這項任務。當然一個項目有很多人開發,之前的功能已經實現了根據文件的URI來上傳具體的File。

問題來了:怎么才能讀取到android res 具體的File呢?


認識res

1. res/raw和assets的區別?

  • 共同點:res/raw和assets這兩個目錄下的文件都會被打包進APK,并且不經過任何的壓縮處理。

  • 不同點:assets支持任意深度的子目錄,這些文件不會生成任何資源ID,只能使用AssetManager按相對的路徑讀取文件。如需訪問原始文件名和文件層次結構,則可以考慮將某些資源保存在assets目錄下。


解決問題

copy
可以對Android 的 res 和 assets 里面的文件進行拷貝。我封裝了兩個方法。

/**
 * copy asset to
 *
 * @param context
 * @param fileName
 */
public static void copyAssetsToOutterByFileName(final Context context, final String fileName) {
    //getFilesDir,指/data/data/<包名>/files/
    final File filedir = new File(context.getFilesDir() + "/classlogo");
    if (!filedir.exists()) {
        filedir.mkdir();
    }

    final File file = new File(filedir, fileName);
    if (file.exists()) {
        Logger.t(TAG).i(fileName + "文件存在,無需拷貝");
        return;
    }
    new Thread() {
        @Override
        public void run() {
            InputStream is = null;
            OutputStream fos = null;
            try {
                is = context.getAssets().open(fileName);
                fos = new FileOutputStream(file);
                //緩存
                byte[] b = new byte[2 * 1024];
                int len;        //每次讀的字節數
                while ((len = is.read(b)) != -1) {
                    if (fos != null) {
                        fos.write(b, 0, len);
                    }
                }
                fos.close();
                is.close();
                Logger.t(TAG).i(fileName + "文件拷貝完成");
            } catch (IOException e) {
                Logger.t(TAG).i(fileName + "文件拷貝失敗");
                e.printStackTrace();
            } finally {
                closeQuietly(fos);
                closeQuietly(is);
            }
        }
    }.start();
}

/**
 * @param context
 * @param fileName
 * @param type     "drawable" "raw"
 */
public static void copyResToOutterByFileName(final Context context, final String fileName, final String type) {
    //getFilesDir,指/data/data/<包名>/files/
    final File filedir = new File(context.getFilesDir() + "/classlogo");
    if (!filedir.exists()) {
        filedir.mkdir();
    }

    final File file = new File(filedir, fileName);
    if (file.exists()) {
        Logger.t(TAG).i(fileName + "文件存在,無需拷貝");
        return;
    }
    new Thread() {
        @Override
        public void run() {
            InputStream is = null;
            OutputStream fos = null;
            try {
                int resId = context.getResources().getIdentifier(fileName, type, context.getPackageName());
                is = context.getResources().openRawResource(resId);
                fos = new FileOutputStream(file);
                //緩存
                byte[] b = new byte[2 * 1024];
                int len;        //每次讀的字節數
                while ((len = is.read(b)) != -1) {
                    if (fos != null) {
                        fos.write(b, 0, len);
                    }
                }
                fos.close();
                is.close();
                Logger.t(TAG).i(fileName + "文件拷貝完成,文件地址:" + file.getAbsolutePath());
            } catch (IOException e) {
                Logger.t(TAG).i(fileName + "文件拷貝失敗");
                e.printStackTrace();
            } finally {
                closeQuietly(fos);
                closeQuietly(is);
            }
        }
    }.start();
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,466評論 25 708
  • 項目中,歡迎頁背景為視頻,就視頻文件到底放在哪個地方這一問題,跟同事觀點有些不合,查閱相關資料,在此記錄。網上關于...
    貝貝ovo閱讀 3,978評論 1 2
  • ¥開啟¥ 【iAPP實現進入界面執行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 6,537評論 0 17
  • View 自定義View中在onDraw()方法中可以設置padding嗎?答案是不能,設置padding后,Vi...
    ElvenShi閱讀 1,908評論 0 0
  • 她那時候還太年輕,不知道所有命運贈送的禮物,早已在暗中標好了價格
    海棠花未眠__閱讀 297評論 0 0