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.

2

2 Answers 2

44
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"); 
Sign up to request clarification or add additional context in comments.

5 Comments

I replaced the string now, i want to save this updated string in that file again ?
You cant save that file because ALL ITEMS in ASSET folder are READ-ONLY but you can load that string directly in WebView
OK, then can i write the html string in a file and save it to assets having extension .html .
@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: try this wv.loadDataWithBaseURL("fake://not/needed", str,text/html","utf-8","");
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 ....

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.