Load html page on webview without using a web server - android

Is there a way to load an html page on a webview without using a web server?
In my android application I have a web server because the user can save the web pages he wants and he can access them offline later.
I tried the assets folder but I cannot modify it at runtime. I can just read files I´ve put there.
To load online or offline I use the methods of the webview:
browser is the webview.
browser.setWebViewClient(new client());
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setDomStorageEnabled(true);
browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
browser.getSettings().setUseWideViewPort(true);
browser.loadUrl(url);
The url can be :
http://stackoverflow.com a normal one
or
http://localhost:8080"; //the web server inside the app
or from assets folder
files:///android_asset/file.html
But how I said, the use can save many pages and see it when he wants.
Is there a method to load the page saved in internal memory on the webview?
URL =this.getApplicationContext().getFilesDir()+"/www/file.html";
browser.loadUrl(URL);
For example: webview.loadString(); ?

Something like browser.loadUrl(Uri.fromFile(f).toString()) should work, for a File object named f pointing to your desired file. Uri.fromFile() will give you a Uri with the correct scheme; toString() gives you the string representation to hand to the WebView. I use this (or a variation) to read files on external storage. AFAIK, it should work fine for files on internal storage as well.

Related

Pdf file is not able to load in android webview but able to load in external browser

I have the webpage created with angular js file. There are pdf files inside the webpage we want to download and show.
when clicking the pdf file, it just show loading and nth happened. Also shouldOverrideUrlLoading method is never called. This method is not deprecated.
We tried to load the url in external browser and open the pdf, it successfully download and open the pdf file.
I would like to know is there any webview setting I missed out?
current webview settings in my implementation is as below . Kindly help. thank you
webView?.settings?.builtInZoomControls = true
webView?.settings?.domStorageEnabled = true
webView?.settings?.allowFileAccess = true
webView?.settings?.allowContentAccess = true
You should try this one might be it work by WebViewClient.
Check out this -- https://stackoverflow.com/a/14201778/18858169
PDF is a file format NOT handled by WebView, it's designed to show web content, not some documents
if you really want to show PDF inside WebView then you should use some web application, which can take a PDF file (or URL pointing on some) and render content of your file in a "web way", thus WebView could print it
for example Google have such web app and you can open PDFs in it using some of below URLs (these may changed)
https://docs.google.com/viewer?url=http://customdomain.com/pdffile.pdf
https://docs.google.com/gview?embedded=true&url=http://customdomain.com/pdffile.pdf
https://drive.google.com/viewerng/viewer?embedded=true&url=http://customdomain.com/pdffile.pdf
note that you are basing on 3rd-party service - your PDF may be online, but 3rd-party companys server-side may be down or changed routing and your hardcoded URL prefix stop working
and if you want to show "real" PDF then use PdfRenderer class, which will give you a set of Bitmaps, which can be shown with ImageView, no WebView needed

How to open internal storage files in webview?

I want to open files into WebView. My all files saved in internal storage. I want to keep my files private. Files type can be PDF, txt and MS-Word etc.
I have tried using Content Provider but it not works for PDF and MS-Word files.
I tried the below code for open in WebView.
String doc= "<iframe src='http://docs.google.com/viewer?url="+Uri.parse(path)+"width='100%' height='100%' style='border: none;'></iframe>";
web.loadUrl(doc);
How can I open internally stored files in webview?
I have tried using Content Provider but it not works for PDF and MS-Word files.
A ContentProvider works equally well for all MIME types.
I tried the below code for open in WebView.
That is using an undocumented, unsupported URL for displaying Web-hosted content in a Google-supplied document-rendering page. That has no ability to access files on a device.
How can I open internally stored files in webview?
For certain types of files — such as plain text, HTML, and some image formats — WebView will be able to display the content directly. Just use loadUrl() with the file URL.
For a few other types of files, you may be able to find third-party JavaScript that you can use. For example, for PDF, you could modify Mozilla's PDF.js to be able to work with local PDFs.
The only general-purpose solution is for you to learn JavaScript and write your own document-rendering code. Put that JavaScript (and related HTML/images/CSS) in your Android app, and have it open the local filesystem content using file URL values.

Android displaying html file with pictures and without internet connections

I would like to load a html file (with pictures and videos) on a Android tablet, however, there is no internet connection, thus I cannot use url for the pictures and stuff....
Is there any free web servers that allow me to do so (displaying the web content at local host)?
Or is there any way I can do it by writing a android app for it?
Thanks!!
Webview's LoadURL method can take local content. using the file uri retrieving the content from your assets and render it that way. Assuming you follow the standard folder structure for a basic HTML website.
And why would you want to ask about "free web servers" if you don't even have a internet connection... All the content has to be loaded locally on the app or downloaded on the fly (assuming you don't download too much.. if its too much then you are better off packaging it in the app).
Without Internet your html page is static page.
You can store the html file in your local assets folder then:
url = "file:///android_asset/" + mName + ".html";
mWebView.loadUrl(url);
and if you also want to show the images in your web page change the source path of images in source code of html page to local folder and pate all the images to that folder.

How do I save a complete webpage displayed in Android's WebView?

I am developing an Android application in which I have a WebView. I want to save the entire webpage loaded in this webview (Html + all resources + images + icons etc) into a folder and zip it and upload it to a server.
If you use WebView's saveWebArchive, then it is saved in archive format. How do I get the Html and images back from this archive? Is there any documentation for the format of this archive?
If I use addJavaScriptInterface to get the html as described here, I still have to copy the images and other resources from the webview cache dir (/data/data/your app package/cache/webviewCache/). However I did not find webview cache dir (/data/data/your app package/cache/webviewCache/) in Icecream Sandwich.
Is there a way to save the entire webpage displayed in webview along with resources in Android?
Thanks
If anyone is still interested, there is an open source project here that can read WebView's archive format (saved with saveWebArchive) and display it back in a WebView.
https://github.com/gregko/WebArchiveReader
I couldn't find details of the actual format yet.
You can use HTML 5 to store pages in the cache and load from the cache itself.
Caching is easy using HTML5, all you have to do is to make and refer a manifest file that holds information on what all elements need to be cached.
http://www.html5rocks.com/en/tutorials/appcache/beginner/
And here is a good way to implement the cache feature in your webview. http://alex.tapmania.org/2010/11/html5-cache-android-webview.html
Aman Gautam

How to access local assets from a remote URL in Android WebView?

To load an asset in HTML, I am using the URL file:///android_asset/my_image.png.
It works when I am loading HTML locally, for instance, using a WebView.loadData() method.
However, I am unable to load a local asset from a remote web site, for instance, when I load a page using WebView.loadUrl("http://example.com/my_page.html"). It shows the page, but the image is not loaded.
How can I fix this problem?
UPDATE:
tarkeshwar mentioned that it is not possible because of security reasons. I understand when you open a web page in a browser, then you as a user are unable to control what the web page is accessing. And it is also a bit different to access local file system when you may read sensitive data of the user. Here I would like just to access application assets.
A workaround could be, to download the page and load it into the WebView using loadData() method. But there might be some security switch to allow WebView to access local assets.
UPDATE2:
I need to support Android 2.3+.
You can't link to a local resource from an external page. That is due to security reasons.
See Pekka's answer for a similar question: How to show local picture in web page?
Extend WebViewClient and override shouldInterceptRequest to load the file locally. The urls would all appear remote but you can selectively load which every you need.
http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldInterceptRequest(android.webkit.WebView, java.lang.String)
Also answered here:
webview shouldinterceptrequest example

Categories

Resources