最开始我读取文件我是这样读的:
is = context.getClass().getResourceAsStream("/assets/infor.txt");
这样造成了2.1以下版本在TextView上面的表现就变成一个大大的空心疑问号;
解决办法是调用AssetManager 来读取文本,如下:
is = context.getResources().getAssets().open("infor.txt", AssetManager.ACCESS_BUFFER);
这样就OK了。
具体读取类:
public class Reader {
/**
* 读取txt
*
* @param str
* @return
* @throws Exception
*/
public static String readText(Context context,int i) throws Exception
{
InputStream is = null;
try {
if (i == 0) {
/*is = context.getClass()
.getResourceAsStream("/assets/infor.txt");*/
is = context.getResources().getAssets().open("infor.txt", AssetManager.ACCESS_BUFFER);
} else {
/*is = context.getClass().getResourceAsStream(
"/assets/infor2.txt");*/
is = context.getResources().getAssets().open("infor2.txt", AssetManager.ACCESS_BUFFER);
}
int index = is.available();
byte[] data = new byte[index];
is.read(data);
String gbk = new String(data, "GBK");
data = GBKTOOL.gbk2utf8(gbk);
return new String(data, "UTF-8");
} catch (IOException e) {
// TODO: handle exception
}finally{
is.close();
}
return null;
}
}