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.
Related
Is there a way for me to edit the contents of the Webview or to crop out some parts of the content, i'm focusing on UI and would like to clean up the content for a project i am working on.
Also lets say I want to display content of the webView individually as well (multiple webviews within an activity) for aesthetic purposes, how would I go about doing that?
One technique I've used in the past is to request the page with an HttpURLConnection rather than the WebView. Then I parse and edit the HTML data and display it in the WebView using the loadData method. You could even use something like JSoup to parse the HTML. However, if you are depending on having the JavaScript run on the page, you may have a problem because using loadData will disrupt the same-origin policy.
I'm running my app that has webview on different activities, they are loading online pages, the issue is that everytime the activity opens the webview takes a couple of seconds to load the page and the page is not available offline.
I was looking for a solution where the webview would always be accessing an offline copy of the website, and the app would download and replace the local website every 24 hours.
I thought it would be simple
Thanks
I see a couple potential solutions to this problem.
Change WebView Cache Mode
The first and simplest would be to change the cache mode of the WebView so that it is forced to load from the cache. This way it will try to load from the cache, and if any resources are not cached, then it will do a network load. This can be simply accomplished by using:
WebView webView; // initialized somewhere
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
Theoretically, you could use LOAD_CACHE_ONLY if the WebView has already loaded once and it doesn't need updating, otherwise you could use LOAD_DEFAULT. Using the LOAD_CACHE_ELSE_NETWORK should definitely speed up loading time, but it might still load some stuff from the network.
However, in practice, I have seen the cache settings not work as consistently as I would like them to.
Utilize WebView.saveWebArchive()
Another more robust solution would be to use the web archive capability that the WebView has. I have not used this solution myself, but you can read about it in this question.
Basically, you can save the contents of the WebView using the following:
WebView webView; // initialized somewhere
webView.loadUrl("http://blahblahblah.com"); // URL to save
Context context = this;
String pathFilename = context.getFilesDir().getPath() + File.separator + "savedWebPage.mht"; // You can use any path you want
webView.saveWebArchive(pathFilename);
Then you can load the saved archive file by using:
webView.loadUrl("file://" + pathFilename);
Please note that I am leaving out the logic for handling when to load from the website and when to load from the archive file, that's up to you how to handle it.
I am working on an android app that displays Web Content that it obtains from a Web site. The app obtains this content from a Web page. The problem is I want to display a particular portion of the Web page, not the whole thing as the whole thing contains excess unwanted content. How can I display the data I need?
you need to download HTML code without WebView first. Then process it removing unwanted data, and then load resulting HTML into WebView
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)
I am working on hybrid app and wanted to know the best practices or good way of architecting/structuring the application.
Should one use one webview per activity or should have one single activity with one just webview and change the html pages that are being loaded?
Can one use one webview on multiple activities?
Certain web pages requires loading of heavy resources such as videoview. Due to this I was thinking of using single webview with different activities if its feasible.
You should go with Single Activity with Single Webview. Wherein load the static resources (Html files , js files images etc. )from local location and go to server for dynamic data.
Call for data from server better to use native layer to avoid cross scripting issue. Having said that , Write all the API's in Java and call each api from WebView either using JavaScriptInterface Mechanism or using ShouldOverrideURL method of webview. Recommended is JavaScriptInterface.
To your answer for question : Its always feasible having one webview with multiple activities. but that's not recommended. One Activity is more than enough to take care of loading complete website with different pages in webview.