12

I have an html file in assets, aaa.html. I want to read the contents of html file and replace it from another string.

Is this way is right or is there any other option.

my code:

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

But its giving file not found, where as loading in web view loads the file.

zaiff
  • 979
  • 2
  • 12
  • 29
  • 2
    http://www.javacodegeeks.com/2012/02/android-read-file-from-assets.html – Dheeresh Singh Jun 22 '12 at 14:15
  • Possible duplicate of [how to read html content from assets folder in android](https://stackoverflow.com/questions/8603222/how-to-read-html-content-from-assets-folder-in-android) – Suhaib Jul 04 '17 at 00:07

2 Answers2

42
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");
MAC
  • 15,363
  • 8
  • 51
  • 92
  • I replaced the string now, i want to save this updated string in that file again ? – zaiff Jun 22 '12 at 14:25
  • 1
    You cant save that file because ALL ITEMS in ASSET folder are READ-ONLY but you can load that string directly in WebView – MAC Jun 22 '12 at 14:34
  • OK, then can i write the html string in a file and save it to assets having extension .html . – zaiff Jun 22 '12 at 14:37
  • @gtumca-MAC how do i load that into a webview with wv.loadDataWithBaseURL("file:///android_asset/html/", str, "text/html", "utf-8", null); - errors occur – SquiresSquire Aug 14 '12 at 09:33
  • @SquiresSquire: try this `wv.loadDataWithBaseURL("fake://not/needed", str,text/html","utf-8","");` – MAC Aug 14 '12 at 09:53
5

If you want to load file in webview then use this

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

you want to replace content inside Html file tags so the solution class code is here..

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 this class just replace string with values from Json hope this solves your problem ....

MobileEvangelist
  • 2,416
  • 1
  • 20
  • 34