Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load local html in WebView?

People also ask

How do I display html content in WebView?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken web view to show html content.

How do I view local html files on Android?

You can use a local web server in your Android phone itself. There are many server apps out there in the Play Store, one such app is Simple HTTP Server. You can put your documents into the folder Android/data/com. android.

Does local storage work in WebView?

html file informs me that local storage is'nt supported by my browser (ie. the webview ). If you have any suggestion.. @Maetschl: if you want to change the code block from intentations to backticks, then at least remove the leading spaces.


You can only do something like that. This solution load HTML from a String variable:

String html = "<html><body>Hello, World!</body></html>";
String mime = "text/html";
String encoding = "utf-8";

WebView myWebView = (WebView)this.findViewById(R.id.myWebView);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadDataWithBaseURL(null, html, mime, encoding, null);

EDIT: try to set the first parameter (the baseURL) of loadDataWithBaseURL() for your needs


public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        WebView view = (WebView) findViewById(R.id.webView1);
        try {
        InputStream input = getResources().openRawResource(R.raw.lights);
        Reader is = new BufferedReader(
                new InputStreamReader(input, "windows-1252"));


            //InputStream input = getAssets().open("ws.TXT");
            int size;
            size = input.available();
            byte[] buffer = new byte[size];
            input.read(buffer);
            input.close();
            // byte buffer into a string
            javascrips = new String(buffer);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // String html = readFile(is);

        view.loadDataWithBaseURL("file:///android_res/raw/", javascrips, "text/html",
                "UTF-8", null);
    }

Try this code. It works for me.

WebView mDesc = findViewById(R.id.descWv);
WebSettings settings = mDesc.getSettings();
settings.setDefaultTextEncodingName("utf-8");
mDesc.loadData(mDescText, "text/html; charset=utf-8",null);

If you want to access localhost through the Android, you need to use http://10.0.2.2:35643/ where 35643 is the specific port, if needed.