Android java open from assets->fast
public static String openAssetFile(Context ctx) {
BufferedReader br=new BufferedReader(new InputStreamReader(ctx.getResources().openRawResourc开发者_运维百科e(R.raw.hung)));
String readLine;
String sout="";
try {
while ((readLine = br.readLine()) != null) {
sout+=readLine;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sout;
}
this not work, its freezes, my xml file is about 300 kb.
how i can handle this?
Try using a StringBuffer like this, the way you are doing it is very slow
public static String openAssetFile(Context ctx) {
BufferedReader br=new BufferedReader(new InputStreamReader(
ctx.getResources().openRawResource(R.raw.hung)));
String readLine;
StringBuffer sout= new StringBuffer();
try {
while ((readLine = br.readLine()) != null) {
sout.append(readLine);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sout.toString();
}
Tried putting it in "/xml" and calling Resources.getXML()
, or putting it in "/assets"?
精彩评论