Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read HTML file from assets

Tags:

android

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.

like image 476
zaiff Avatar asked Jun 22 '12 14:06

zaiff


People also ask

How do I open an asset in HTML?

Right-click the assets folder and then click New > File. Enter index. html and press Return or Enter .

How to Open HTML file in android java?

Method 1. Step 1: To add a local HTML file into your Android project there must be an asset folder in it. To create an asset folder in Android studio open your project in Android mode first as shown in the below image. Step 2: Go to the app > right-click > New > Folder > Asset Folder and create the asset folder.


2 Answers

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");
like image 189
MAC Avatar answered Nov 14 '22 11:11

MAC


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 ....

like image 5
MobileEvangelist Avatar answered Nov 14 '22 11:11

MobileEvangelist