Android java从assets->快速打开

时间:2010-11-08 13:08:35

标签: android assets

public static String openAssetFile(Context ctx) {


BufferedReader br=new BufferedReader(new InputStreamReader(ctx.getResources().openRawResource(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;
}

这不起作用,它的冻结,我的xml文件大约300 kb。

我怎么能处理这个?

2 个答案:

答案 0 :(得分:1)

尝试使用像这样的StringBuffer,你的方式非常慢

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

答案 1 :(得分:0)

尝试将其放入“/ xml”并调用Resources.getXML(),或将其放入“/ assets”中?