Vassily
Vassily

Reputation: 5428

How to write html file to local storage in android and then read it?

I have an html string:

String htmlString = "<h1>Hello World</h1><p>DaParagraph</p>"

I want to save it as file.html to internal/external storage and then read it. What should I do?

Upvotes: 0

Views: 903

Answers (2)

Jay Donga
Jay Donga

Reputation: 380

Alternative easy option.

You should store that htmlstring to either SQLite database or SharedPreference as string.

and load that string as html in webview

String htmlString = "<h1>Hello World</h1><p>DaParagraph</p>";
                webview.getSettings().setJavaScriptEnabled(true);
                webview.loadDataWithBaseURL("", htmlString, "text/html", "UTF-8","");

Upvotes: 2

AndiGeeky
AndiGeeky

Reputation: 11474

1. Please save your html which is string response from server in shared preferences like this :

SharedPreferences preferences= PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("HTML", "your html string");
        editor.commit();  

2. Get this String wherever you want to use :

SharedPreferences preferences= PreferenceManager.getDefaultSharedPreferences(context); 
String htmlString=preferences.getString("HTML", null);

3. Convert String to html :

Html.fromHtml((String) htmlString).toString();

Happy Coding.!!

Upvotes: 1

Related Questions