Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading local html file in webView android

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());      } 
like image 419
donadev Avatar asked Jan 01 '14 22:01

donadev


People also ask

How do I open an HTML file in WebView?

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

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.

How do I enable file upload in WebView?

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.


2 Answers

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"); 
like image 24
Nauman Ash Avatar answered Oct 21 '22 05:10

Nauman Ash


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"); 
like image 162
hichris123 Avatar answered Oct 21 '22 03:10

hichris123