从资产中读取HTML文件

时间:2012-06-22 14:10:23

标签: android

我在资产中有一个html文件,aaa.html。 我想阅读html文件的内容并将其替换为另一个字符串。

这种方式是对的,还是有其他选择。

我的代码:

File f = new File("file:///android_asset/aaa.html");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);

但是找不到给定文件,在web视图中加载文件时加载文件。

2 个答案:

答案 0 :(得分:38)

InputStream is = getAssets().open("aaa.html");
int size = is.available();

byte[] buffer = new byte[size];
is.read(buffer);
is.close();

String str = new String(buffer);
str = str.replace("old string", "new string");

答案 1 :(得分:3)

如果要在webview中加载文件,请使用此

mWebView.loadUrl("file:///android_asset/myfile.html");

你想要替换Html文件标签内的内容,所以解决方案类代码就在这里..

public class CardDetail {
    public static String newHtmlString = "";

    // private Context context;

    @SuppressWarnings("rawtypes")
    public String getNewHtmlString(String htmlString, HashMap hm) {
        try {
            StringTokenizer st = new StringTokenizer(htmlString, "##");
            CardDetail.newHtmlString = "";
            while (st.hasMoreTokens()) {
                String token = st.nextToken();
                CardDetail.newHtmlString += token;
                if (st.hasMoreTokens()) {
                    String token2 = st.nextToken();
                    if (token2.equals("NAME") || token2.equals("POSITION") || token2.equals("COMPANY") || token2.equals("PHOTOURL"))
                        CardDetail.newHtmlString += hm.get(token2);
                    if (token2.equals("SKYPE_CONTAINER1")
                            || token2.equals("TWITTER_CONTAINER1")
                            || token2.equals("PHONENUMBER_CONTAINER1")
                            || token2.equals("EMAIL_CONTAINER1")
                            || token2.equals("ADDRESS_CONTAINER1")) {
                        String replaceString = st.nextToken();
                        String tokenMiddle = (String) hm.get(st.nextToken());                       
                        if (!tokenMiddle.equals("")) {
                            replaceString += tokenMiddle;
                            CardDetail.newHtmlString += replaceString   + st.nextToken();
                            st.nextElement();
                        } else {
                            st.nextElement();
                            st.nextElement();
                        }
                    }
                }
            }
//          Log.i("convertedHTMLString", newHtmlString);
            return CardDetail.newHtmlString;

//          htmlString = "<img src='" + hm.get("PHOTOURL") + "' width=80 height=80>";           
//          return htmlString;
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    public HashMap<?, ?> getProfileHashMap(JSONObject jsonObject) {
        @SuppressWarnings("rawtypes")
        HashMap hm = new HashMap();
        jsonObject = (new JSONConverterClass()).convertJsonObjectToCardDetail(jsonObject);
        try {
            hm.put("EMAIL", jsonObject.getString("email"));
            hm.put("NAME", jsonObject.getString("firstname") + " " + jsonObject.getString("lastname"));
            hm.put("COMPANY", jsonObject.getString("company_name"));
            hm.put("POSITION", jsonObject.getString("position"));
            hm.put("WEBSITE", jsonObject.getString("website"));
            hm.put("PHONENUMBER", jsonObject.getString("phonenumber"));
            hm.put("PHOTOURL", jsonObject.getString("picture_url"));
            hm.put("SKYPE", jsonObject.getString("skype_username"));
            hm.put("TWITTER", jsonObject.getString("twitter_username"));
            hm.put("ADDRESS", jsonObject.getString("generic_location"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hm;
    }
}

convertJsonObjectToCardDetail这个类只是用Json中的值替换string 希望这能解决你的问题......