【代码是最好的老师】
1.概述
- res/raw目录下的文件不会被压缩,会以原始文件打包进apk文件
- 文件资源可以在代码中直接引用,如R.raw.ic_halo或xml中@raw/xx
- app内置音效、短提示音等建议放此处
- res/raw不支持多级目录,如果需要自由度,推荐使用assets方式
2.使用
读取文件内容:
读取图片文件
2.1 读取文件内容
private void readbtnReadRawFile() {
String result = "";
try {
InputStream is = getResources().openRawResource(R.raw.text);
int lenght = is.available();
byte[] buffer = new byte[lenght];
is.read(buffer);
result = new String(buffer, "utf8");
} catch (Exception e) {
e.printStackTrace();
}
mTv.setText("res/raw/text.txt:\n"+result);
}
2.2 读取图片
private void readbtnReadRawImg() {
Bitmap bitmap = null;
try {
InputStream is = getResources().openRawResource(R.raw.ic_logo);
bitmap = BitmapFactory.decodeStream(is);
} catch (Exception e) {
e.printStackTrace();
}
if (bitmap != null){
mImgShow.setImageBitmap(bitmap);
}
}