Android – Load local html in WebView

androidwebview

I want to load a local html into a WebView WITHOUT using "file:///" because that does not allow cookies. Is there a way to use something like "localhost" ?

Secondly, I could not find a way to enable cookies in the getSettings(). Because cookies are not allowed while using "file:///".

Best Answer

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