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