I have to load an existing html file into a WebView
that is located at this path in the file system:
/data/data/com.example.example/files/file.html
But, when the WebView
loads it, I don't see anything. Who can help me?
WebView
code (assuming path is the path I've written above):
WebView webView = (WebView)findViewById(R.id.webView1); File htmlFile = new File(path); if(htmlFile.exists()) { webView.loadUrl(htmlFile.getAbsolutePath()); }
The WebView method to load our file takes a URI , so we need to access the HTML file using that URI . Since we stored it in the assets folder, we can access it using file:///android_asset/{file_name} .
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.
According to comment here, add android:usesCleartextTraffic="true" in application tag (manifest file) to make it work in Pie. Working absolutely fine on 9.0.
The html file should be placed in the assets folder, which will belong in the root directory of your project.
So move your file to in case of eclipse
assets/index.html
In an Android Studio project use this folder:
/app/src/main/assets/index.html
Now use
WebView wv= (WebView)findViewById(R.id.webView1); wv.loadUrl("file:///android_asset/index.html");
Try this, adding in a file:///
and doing it a little differently:
WebView webView = (WebView)findViewById(R.id.webView1); webview.loadUrl("file:///data/data/com.example.example/files/file.html");
Instead of this, however, you could just put the file into your assets
folder in the source code, and then do this:
WebView webView = (WebView)findViewById(R.id.webView1); webview.loadUrl("file:///android_asset/file.html");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With