软件编程
位置:首页>> 软件编程>> Android编程>> Android 读取资源文件实例详解

Android 读取资源文件实例详解

作者:lqh  发布时间:2022-08-28 13:31:17 

标签:Android,读取文件

Android 读取资源文件实例详解

本文主要介绍 Android 读取资源文件,直接从 assets 读取,从 Raw 文件中读取,InputStream 转 String。

以下为直接从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;
}

以下为从Raw文件中读取:


/**
* 读取raw文件夹下面的文件
* @return
*/
public InputStream getFromRaw() {
InputStream ins = null;
try {
 ins = getResources().openRawResource(R.raw.area);
} catch (Exception e) {
 e.printStackTrace();
}
return ins;
}

下面是 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;
}

 

 

来源:http://www.123si.org/android/243.html

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com