Get already loaded images from wevbiew - android

I want to get images from the web page and save them into the local storage. I can find all image URLs from the HTML, load that images from the server and save them. But I don't want to access to the server second time for the same information which is already in webview. So I want to know how I can load images from the webview?

You can set a WebView cache in a few steps. Take a look below.
WebView webView = (WebView) findViewById(R.id.your_webView_Id);
String cacheDir = getDir("your_WV_cache_dir", Context.MODE_PRIVATE).getAbsolutePath();
webView.getSettings().setAppCacheMaxSize(1024 * 1024 * 1); // 1 Mb cache limited size > webView.getSettings().setAppCachePath(cacheDir);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); // This constant is important to you. This way you can load daa from cache, if it was already downloaded once.
You might take a look to th other possible values to the constant above at the WebView.WebSettings official documentation.
Let me know if it works.

Related

Android Webview loading slow for the first time

I have created a library to start an activity with a Webview.
I'm trying to load local HTML file which with an Iframe inside it Whenever I'm starting the activity, for first time it is taking approx. of 5 seconds to display iframe. The 2nd time it is taking approx. of 2 seconds to display.
How can i reduce load time, for the first load?
Here are the two java files respectively.
WebviewOverlay.java
String botUrl = "someurl";
myWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
myWebView.loadUrl(botUrl);
return myWebView;
WebviewActivity.java
wb = new WebviewOverlay();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().add(R.id.container, wb).commit();
This one is not anything to do with Android, web page has lot of data and or may be high resolution images which taking time or any scripts on the page which taking time while loading
You can use precaching to speed up the webpage load.
Load data(HTML) from the server and save it to a text file in our device memory.
Start a coroutine to load the webpage and store in local storage
Create a new activity in which we will create a webview which will be used to load this cached webpage.
Change some settings of our webview to enable support for javascript, domstorage, database etc.
Attach webclient to our webview
Load our previously created file which contains all our HTML cached and load it to the webview
Here's a Github repo that does exactly what you want it to do.
https://github.com/iambaljeet/WebViewPreCache
Try using optimized images in your Webpage. Resize it from mspaint

is it possible to save/retrieve specific data from webview cache?

i load a web page in a webview that contains a form, i want to know if there is a way to save the data that user put in the form in the webview cache and auto fill it in his next visit?
this is all i have done for now.
webView.getSettings().setAppCachePath("/data/data/"+ getPackageName() +"/cache");
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
not sure if it's the right way but, what about running js script on webview to get form input values, navigating DOM tree of html or using selector with id ?
once you got them you can store them in your app and for repopulate, before loading the page in webview, you manipulate html (if you could) or you use another js script to set the values
i'm not sure but i thing cache is only for cachable values (images, css...) not for input

How to load cached images while use webview.loadData(...) without network

Help me please.
I load html data into webView using loadData(...).
This data contains <img src='http://external.com/some.jpg'/>
I want this image to be loaded from web if network is available, or to be used from cache if network is unavailable.
I'm not using javascript.
I prefer to use mechanism within webView or android.
//First write logic of caching image using
mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
mWebView.getSettings().setAppCachePath(....)
//Although I do not recommend using above method. Cache image using alternative method.
if(check internet connection method)
+ "<img src=\"http://external.com/some.jpg\">"
else
+ "</img>
+ "<img src=\"file:///android_asset/some.jpg\">"
+ "</img>
//android_asset folder if file is shipped with apk or path captured above

How to improve webview load time

I have a URL which I can load in webview. If I open the webview through browser it takes less time, but if I load the URL in webview it takes more time. Also if I have to reuse the same webview page with the same URL in another screen then I am unable to do that because if I use the same webview object in different screens then also it takes time to load the URL.
How do I make my webview to load a URL instantly?
You could use the YSlow or Google PageLoad plugins for your browser, to get specific tips on how to improve page load and speed up your page.
You could try a different cache mode: http://developer.android.com/reference/android/webkit/WebSettings.html#setCacheMode%28int%29
or, if the page doesn't change, just use loadDataWithBaseURL:
http://developer.android.com/reference/android/webkit/WebView.html#loadDataWithBaseURL%28java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String%29
By default, a WebView provides no browser-like widgets, does not
enable JavaScript and web page errors are ignored.
here i paste small part of code of zirco-browser
settings.setJavaScriptEnabled(Controller.getInstance().getPreferences().getBoolean(Constants.PREFERENCES_BROWSER_ENABLE_JAVASCRIPT, true));
settings.setLoadsImagesAutomatically(Controller.getInstance().getPreferences().getBoolean(Constants.PREFERENCES_BROWSER_ENABLE_IMAGES, true));
settings.setUseWideViewPort(Controller.getInstance().getPreferences().getBoolean(Constants.PREFERENCES_BROWSER_USE_WIDE_VIEWPORT, true));
settings.setLoadWithOverviewMode(Controller.getInstance().getPreferences().getBoolean(Constants.PREFERENCES_BROWSER_LOAD_WITH_OVERVIEW, false));
settings.setSaveFormData(Controller.getInstance().getPreferences().getBoolean(Constants.PREFERENCES_BROWSER_ENABLE_FORM_DATA, true));
settings.setSavePassword(Controller.getInstance().getPreferences().getBoolean(Constants.PREFERENCES_BROWSER_ENABLE_PASSWORDS, true));
settings.setDefaultZoom(ZoomDensity.valueOf(Controller.getInstance().getPreferences().getString(Constants.PREFERENCES_DEFAULT_ZOOM_LEVEL, ZoomDensity.MEDIUM.toString())));
settings.setUserAgentString(Controller.getInstance().getPreferences().getString(Constants.PREFERENCES_BROWSER_USER_AGENT, Constants.USER_AGENT_DEFAULT));
set render priority high for your web view can solve problem up to some extent
its some thing like this
webview.getSettings().setRenderPriority(RenderPriority.HIGH);

Android: Easiest way to make a WebView display a Bitmap?

I have some images that I loaded from a remote source stored in Bitmap variables and I want to display them. In addition to switching between these images the user should also be able to zoom and pan them. My first idea was to somehow pass them via an intent to the built-in gallery application but this doesn't seem to be possible.
A solution that is suggested in several places is using a WebView since it already supports zooming and panning.
My question is how does my Bitmap data get into the WebView? Do I have to write it to a file first, which I would have to remove again later, or is there an easier way?
Or are there even better ways to accomplish my main goal, which is displaying Bitmap data as zoomable and panable images?
You can just use webview to directly view your image remotely. You do not need to save anymore the image in a file.
Here is a sample code snippet.
myWebView.getSettings().setBuiltInZoomControls(true); //to get zoom functionalities
String url = "http://....."; //url of your image
String x= "<html><head><meta name=\"viewport\" content=\"width=device-width, minimum-scale=1.0\"/><style type=\"text/css\">html, body {margin: 0;padding: 0;} img {border: none;}</style><head><body style=\"background: black;\"><table><tr><td align=\"center\"><img src=\"" + url + "\" /></td></tr></table></body></html>";
myWebView.loadData(x, "text/html", "UTF-8");
About switching images, you can just change the value of the url and call the loadData again of the webview.
I wasn't satisfied with WebView after all so I ended up creating my own image viewing Activity. Further descriptions on how I did it can be found in this post on google groups.

Categories

Resources