Android 读取资源文件
程序员文章站
2022-04-28 15:43:51
...
主要介绍 Android
读取资源文件,直接从 assets
读取,从 Raw
文件中读取,InputStream
转 String
.
1. 从 assets
读取:
/**
* 得到Assets里面相应的文件流
*
* @param fileName
* @return
*/
private InputStream getAssetsStream(String fileName) {
InputStream is = null;
try {
is = getAssets().open(fileName);
//is.close();
} catch (IOException e) {
e.printStackTrace();
}
return is;
}
2. 从 Raw
文件中读取:
/**
* 读取raw文件夹下面的文件
* @return
*/
public InputStream getFromRaw() {
InputStream ins = null;
try {
ins = getResources().openRawResource(R.raw.area);
} catch (Exception e) {
e.printStackTrace();
}
return ins;
}
3. 下面是 InputStream
转 String
/**
* InputStream 转String
* @param inputStream
* @return
*/
private String InputStreamToString(InputStream inputStream) {
String result = null;
try {
int length = inputStream.available();
byte [] buffer = new byte[length];
inputStream.read(buffer);
result = EncodingUtils.getString(buffer, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return result;
}