So that's about it, I want webview to download and cache multiple pages when the app is opened so that they're saved for offline use. They're only going to be a few kilobytes each and there's only going to be about 10 of them, we're talking less than 100kb all together, but I want to make sure all the info is up to date when the app is open. How would I do go about doing this? Just somewhere to start would be nice.
Edit: assume internet connection at first, but then this stops and there's no internet after the caching is complete.
You can create a WebView and make it not visible and load the resources here.
Related
I have been trying to enable caching in Android WebView but every config I saw on SO ends in failure.
Here is what I have:
I have a webpage which has an Iframe inside that loads 10-12 pages in a loop all the content in those pages have Cache-Control: max-age=86400 header.
If I load these pages normally in a browser they work and the pages are cached.
So here is the config that I have
getSettings().setJavaScriptEnabled(true);
getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
getSettings().setDomStorageEnabled(true);
getSettings().setMediaPlaybackRequiresUserGesture(false);
getSettings().setAppCacheEnabled(true);
getSettings().setAppCachePath(context.getCacheDir().getAbsolutePath());
getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
These settings work for media playback and enabling js
EDIT
I built a new app and tried loading google with the loadUrl method and reloaded that with a button while the net was off, it was able to reload the page without internet it means it cached that page.
So the issue is that webview is not caching pages which are being loaded by the page.
If anyone knows how we can cache the content which is loaded by webview page loads it would be a great help.
-Thanks
So after a series of detailed tests on our iframe and Android device, I came to realize that WebView does not cache files which are bigger than a particular size.
One of my pages which were being loaded by the iframe had a video of 30mb which was not getting cached and was resulting in huge data consumption. (as it was getting rendered every 2 mins)
I reduced it to 10mb and it still did not cache it finally I reduced it to 980kb and it started caching.
I am not sure about the caching limit per file for WebView but I came across an interesting bug here which states after Android 4.3 the total cache limit was hardcoded to 20mb which we can find in this file on line 98.
If anyone knows where these limits are documented do let the community know by replying here.
This code solve my problem, hope help you too:
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
EDIT: Also you don't needs to these lines:
getSettings().setAppCacheEnabled(true);
getSettings().setAppCachePath(context.getCacheDir().getAbsolutePath());
Remove them and check one more time. AppCache setting not related to type of cache that you want.
I am making a network app that works with a few news sites. I have an option to view and download articles (for reading in future). There is no fast mobile internet in my country (mostly 2G with the speed up to 15 KB/sec) and my app doesn't work really well with the slow connect though it's perfect with fast connection. As I have seen from logs it can't establish the connection. When I try to open the article it doesn't load at all or goes into infinite loading. When I try to save the article it simply saves the blank file. How can I track these mistakes and catch them? And what are the correct ways of handling it?
I suppose that my code is irrelevant as it works correctly with Wi-fi. I think I simply need to add lines, not to edit them
ok, you need to manage the information very good, you need define the package size to download, you need a webservices that allow to you to manage the information one by one, for example you need design packages no more than 500kb size to download, when this package is downloaded you can show something in your mobile and continue downloading the next package in background.
500kb is only an example you need to test what is the maximum data size to download and create packages like that in your web services
As the title implies, could anybody explain if and why it is required to clear webView cache in Android?
My concern is that the cache piles up endlessly and that doesn't sound a good idea. On the other side, it helps loading pages faster, so I prefer not to clear that cache but I wonder what is the standard? What is acceptable here?
Is it the same for clearing history?
Additionally, is there a way to clear the cache/ history in a certain time interval or as it reaches to a certain size limit?
Thank you.
I don't think that cache "piles up endlessly" - every sane cache mechanism that I've ever seen maintains a maximum size - usually as an MRU cache.
So, you shouldn't have to maintain it yourself.
If, for some reason, you really do need to clear the cache, however, you can use the WebView's clearCache method
In my webview project, the webview [in a fragment] was going to the last page visited -- from the cache -- on startup. This was no good, so, I clear the cache: mWebView.clearCache(true);
Now, it would be nice to clear the cache on program exit; then, you wouldn't even be storing cache when not running the app. But, that seems not possible, because, if you put the cache clear code in onDestroy(), then it gets cleared every time the device is rotated, so you lose benefit of having the cache. So, I do the clear cache as the first operation after the webview is initialized.
I'm pretty new at this, so, don't take my experience as gospel.
I have a listview and each listview item when clicked opens a gallery (conating 30 + images ) to be downloaded (using the urls).I don't want to re - download the images on next visit and also want the images to be present when i click on list in offline mode.
I went through several links in SO and am a bit confused on which approach to follow:
1> Use bytearray and store the images in sqlite db.
2> use context -> getExternalCacheDir() to store it on the
external memory (this wont work on devices with no external
memory.)
3> Use SoftReferences as suggested in following link
http://android-developers.blogspot.in/2010/07/multithreading-for-performance.html
Please help to guide me on the best approach to handle around 1000 + images which works in offline mode as well.
1000 images.....I sure hope they're thumbnails or you're going to end using a lot of storage space.
You should really only cache what you need and what images are used repeatedly. Both options 1 and 2 are dependant on the phone even having storage space, if there's no space then you won't be caching much to disk. 3 won't store images permanently, if the app is put in the background and gets killed and so will the cache.
It's hard to say what the correct decision here is without knowing the inner workings and requirements of the app.
Just going by what you want then I would say go with several solutions
If there's external storage write to that, if not use internal storage, if there's not enough space internally use the soft references.
I have always found that out of the box caching solutions never fit quite right with my apps and wind up writing my own cache. Sorry I can't give a better answer.
PS. If you're not careful with 3 you might download too much and run into an OutOfMemoryException.
PPS. 3 can also throw a RejectedExecutionException which is caused by too many async tasks being started at once (eg scrolling quickly through a listview that gets an image via asynctask)
I am building the application that will load list of news from the website. Each news/headline has an image. I want to save/cash the images so user does not has to download them again.
Q: In your opinion, what would be a better/more sufficient way: Loading images and saving them on the device or use the CacheManager? At the moment I am using the first solution and everything works fine. However, the website has many categories and even more news per category therefore there are lot of images saved on the device. Is it normal in this type of applications to save the images on the device?
Thanks for your help,
marqs
I don't think you should save the images on the device, because of many reasons:
Why wasting the device space on news images? All the user wants is to read the news and thats it. (In your case maybe open it later, but still - not forever)
You can save it on the device and make the app. delete those files after lets say 24 hours..
The main issue is the privacy issue, when the user is deleting the cache files he thinks all the webs he visited has wiped from the device, but in this case they aren't..
Maybe you can just add a "Clean Cache" button in the app. but after all I wrote I think using the Cache-manager is the best way - just because it was meant for those things exactly..
:)
Rotem
I didn't find a reason to use CacheManager. I used getCacheDir and stored everything on file. I have two levels of cache. First when I fetch it, I store in memory and disk. When in memory gets bigger than 30 objects, I started clearing the memory to make some room for the new images coming. However, I still keep the images around on disk and bring in to memory as needed. I found this to give me the smoothest scrolling. After about an hour, I start expiring the image on disk too.