日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

android raw 引用,Android 资源-raw和assets

發(fā)布時(shí)間:2024/9/19 Android 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android raw 引用,Android 资源-raw和assets 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前言

這幾天遇到了一個(gè)坑!關(guān)于resource相關(guān)的操作。具體的需求是:替換所有班級(jí)的logo,但是移動(dòng)端android和ios沒有建立協(xié)議,所以臨時(shí)采用的是直接上傳圖片來(lái)完成這項(xiàng)任務(wù)。當(dāng)然一個(gè)項(xiàng)目有很多人開發(fā),之前的功能已經(jīng)實(shí)現(xiàn)了根據(jù)文件的URI來(lái)上傳具體的File。

問(wèn)題來(lái)了:怎么才能讀取到android res 具體的File呢?

認(rèn)識(shí)res

1. res/raw和assets的區(qū)別?

共同點(diǎn):res/raw和assets這兩個(gè)目錄下的文件都會(huì)被打包進(jìn)APK,并且不經(jīng)過(guò)任何的壓縮處理。

不同點(diǎn):assets支持任意深度的子目錄,這些文件不會(huì)生成任何資源ID,只能使用AssetManager按相對(duì)的路徑讀取文件。如需訪問(wèn)原始文件名和文件層次結(jié)構(gòu),則可以考慮將某些資源保存在assets目錄下。

解決問(wèn)題

copy

可以對(duì)Android 的 res 和 assets 里面的文件進(jìn)行拷貝。我封裝了兩個(gè)方法。

/**

* 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 + "文件存在,無(wú)需拷貝");

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; //每次讀的字節(jié)數(shù)

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 + "文件存在,無(wú)需拷貝");

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; //每次讀的字節(jié)數(shù)

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();

}

總結(jié)

以上是生活随笔為你收集整理的android raw 引用,Android 资源-raw和assets的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。