In an Android WebView, I would like to display websites and download the full HTML with all images from those websites to the device's storage.
As the WebView downloads all assets of a page in order to be able to display it, it would be redundant work if I loaded the page in the WebView and afterwards download the HTML and images again, right?
So I thought you could maybe access the contents that the WebView downloaded and just copy them to the device's storage. Is this possible?
According to this page, you can set up JavaScript interfaces and then call some JavaScript statements like this:
webView.loadUrl("javascript:doSomething()");
So I could get the page's HTML if I just used JavaScript's document.innerHTML or document.getElementById('theID').innerHTML.
And for the images, is there an easier solution than to use JavaScript? The problem is that I don't just want the URLs but the loaded resources. The WebView did already download all assets, so the question is if it exposes access to them in some way.
As described in this question, it seems to be possible to get images using context menu events. (Maybe even background images.) But is there a solution that does not require user actions and saves all images in batch, preferably without downloading them once more?
In short: no. There's no API to access the downloaded resources from the WebView from Java. You'll need to roll your own solution.
In the case of images, the only way I can think of to avoid re-downloading the resource would be to write some javascript that copied the image once it was loaded into a <canvas> element and then read the bytes back as a data URL. You'd then be able to recreate the image on the Java side from that data URL (by passing it to Java via a JavaScript interface in your WebView) and save it to disk or work with it otherwise.
These links would probably be helpful to you:
Canvas and toDataUrl:
http://www.html5canvastutorials.com/advanced/html5-canvas-get-image-data-url/
WebView JavaScript Interface: http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object,
java.lang.String)
Related
I have 20+ html pages stored in asset folder in android. These html pages belongs to my website. And all the pages are at different depth in asset folder. (I am writing after 3-4 hours of searching on Google, SO, etc. but no clue! And I am new to android, so need help..)
Now when user types something related to the html pages in edittextview, then that bunch of words will be split in array and then all the words will be matched from all the html pages.
And if most of the words are matched then, user can see that particular webpage (html page) in webview.
How can I achieve that?
I did as said in http://www.monocube.com/2011/02/08/android-tutorial-html-file-in-webview/ but no clue!
And even did https://stackoverflow.com/a/24670620/5738881 but not what I am in need of!
Thanks.
A solution to this is too complicated to write here, but you will have to include some javascript with the HTML that will do the manipulation of the DOM. You have no access to the DOM from Java.
What you can do is call a javascript method in a page loaded into a WebView by loading a javascript url with a function call into some js entry point into the WebView via loadUrl. You can call back into Java from javascript using a mechanism that will require some study. Start by looking at addJavascriptInterface.
I'm developing a simple Android App where the user must fill in a very complex form, for which I believe it's much easier to use an HTML form than an Android Activity with tons of TextViews.
The data collected by the form must be sent to some remote database, and the application must be able to work offline.
I thought of two alternatives, the question is: which one would be better?
Let a WebView load a remote website with an offline manifest
Let a WebView load a local website in assets folder
My second question is related to the storage when offline, and once again I have two options, and I don't know which one is better:
Using the HTML5 local storage, and let HTML + javascript send data to the server when online again
Let my Android app catch the form data, and handle everything the Android way.
Any input will be very helpful. Thanks in advance.
Regarding the first question: depends on how often will you need to update your form. An online cached form can be updated quickly, while bundled pages are only updateable together with the app, and you will need to consider that both legacy and new clients can connect to your server at the same time (users will procrastinate updating).
Another aspect is portability. Do you envision an iOS version of your app, or perhaps a mobile site? If yes, then an HTML5 solution is definitely more portable. Also, debugging an app which is entirely HTML or entirely native is usually easier than a hybrid one -- you can stay within a single debugger.
Perhaps, one drawback of using HTML local storage inside WebView is that the data you save will be in a kind of a "black box" -- you will not be able to back it up easily.
[Added later] OK -- one drawback of putting your site into assets folder is that you'll have to use file: scheme in order to access it. This can lead to some cross-origin loading access related issues if you will try to mix your bundled content with content from the web. Check these WebView settings for example: setAllowFileAccessFromFileURLs, setAllowUniversalAccessFromFileURLs, setMixedContentMode.
Is it possible to cache a list of website urls, image urls etc without opening them first in a webview? I have a list of img urls coming from a feed that i want to cache on download. i have already posted a question about this but have gotten no response here is my previous question. (https://stackoverflow.com/questions/21831464/cache-image-urls-android)
It's not possible to drop things into the WebView's cache directly. The only way that would not require preloading in an "off-screen" WebView (which is totally possible, btw.) would be to download the images yourself and serve them back to the WebView using shouldInterceptRequest.
I would recommend caution with the shouldInterceptRequest-based approach though. It's easy to hurt performance when using shouldInterceptRequest (see here). Also, you need to take care of removing stale images from your 'cache'.
I want to pre load a web page in Android. The web page contains both text and graphical elements. The web page will be displayed in the future in an Activity which has not been created yet.
As far I understand it a WebView for example has to be tied to an Activity, thus it's not possible to use a WebView for this task.
Anyone got any suggestions which does not involve parsing the html page and downloading all the elements "manually"?
According to android's documenation: For obvious security reasons, your application has its own cache, cookie store etc.—it does not share the Browser application's data.
So we can make use of the above info in a smart way by doing this:
Whenever you want to start loading your website, create a new WebView object and request the url from it. the code will look something like this:
WebView view = new WebView(context);
view.loadUrl(myBigWebSite);
Android will cache that webview data and when you request loadUrl from another activity it should be already loaded.
// in your WebView activity
myWebView.loadUrl(myBigWebSite);
For more about webViews visit the documentations page : WebView doc
You can download all required files (images, etc) into some folder on a network thread and then use something like
webview.loadUrl("file:///my_cached_folder/index.html");
Images will also be picked from this folder, if referenced and present. Read about opening local files on webview here.
I am managing a bunch of PDF files in an android application maintaining a list of records in a SQLite database as well as storing the pdf files on the external storage.
Now I would like to present a thumbnail of the first page of the pdf in my list view as part of each cell representing a pdf.
I am aware of libraries like iText, fop.. on the JavaSE side that can render a PDF but I would rather not delve into embedding a large library like that. On a similar approach I would also rather not embed a native PDF viewer like droidreader, apv or vudroid.
Otherwise I could of course also get it rendered on a server via some webservice but that is a lot of headache as well.
I am already using intents to get the pdf's displayed for the user so I was thinking it would be great if I could get a thumbnail via a intent call as a result somehow. However I found nothing on the web (e.g. on openintents) that indicates something like that exists ..
So I am a bit at a loss on what to do? What do you think is the best approach to get these thumbnails into my app? Are there any public intents available? Or did I just totally miss something and the SDK provides features for that already (it should imho but currently does not)?
You are going to get a lot faster resopnse rasterizing the PDFs on the server and there are lots of libraries to do this in C, Java, Php.