I am showing a website within a WebView in an Android app. Since this is a particular page which my app has to access frequently, I would like to offer a cached version of it for when the user has no connectivity. So if the user has an internet connection, the website would be accessed from the internet and a copy would be made. IF you access again but have no connectivity, the cached version should be shown.
My app is for API version 9 and above.
My question is, how would you go and download all assets from a particular page?
The webview is already doing that for you once the user visits that view. The page is loaded and cached. You can set the webview to cache on the sd card like this:
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
link to docs
If this default behaviour isn't enough for you you can look into fetching the content with a httpclient and storing that information. But then you'll have to adapt your webview to go look there first and you'll have to make sure you download all the resources that webpage needs.
So these are two questions you are asking. The one bit regarding WebView and cache was already answered.
Regarding this bit "My question is, how would you go and download all assets from a particular page?", check these links :
How to create web crawler in java?
Simple web crawler on android
How to save file from website to sdcard
First link is a crawler implementation in Java, the second link is basically that code being used (somewhat badly) on Android, the third link should give you enough info as how to download files.
Hope this helps
Related
Im new with Android. My task is to create a mobile application based on a responsive WordPress website. I would like to be able to download some of the content of this site. Website uses HTTPS. In the first, most primitive version, I tried to use WebView. I wanted to use the following: WebView load website when online, load local file when offline. Unfortunately, only the white page appeared and nothing more. In the case of "Google.com link" a page has been loaded in an external browser (Oreo Android).
Any suggestion how should I start? The easiest way would be to download an HTML page or have access to files on the server. Unfortunately, I do not have that access.
As always -1 without even trying to help....
As I said load from cache when Internet is disabled not working. Work only WebView (without login, because CORS). So how should I face it?
I have an app which contains html files which i am displaying using a webview. This html files are nothing but documentation the users of my app require. My question is, can I update these html files without asking the user to update the entire app? I dont want to replace these html files by hosting the documentaion online.
You will have to do one of these:
Update your app (which is what you don't want)
Host the raw .html files on a web server, and whenever the user opens the app, check if there are new files available and download them.
Simply create a website that shows the .html files and open the
website in your app (which i'm guessing is what you don't want
either)
So you're left with option 2.
To update files without requiring an app update, you will need the app to obtain the HTML content online.
One option, which you mentioned you don't want, is loading a URL into the WebView rather than local HTML files.
The other option would involve hosting the web pages somewhere and the app would download them. The only benefit of this I can see, is that if the device is offline is can use the last obtained HTML file. You could just use caching to achieve this though.
How complicated are the webpages? Using JSON is another method.
The core part of my application is load PDF online and user have access to download it as per their given rights.
The problem I am facing is that I want to load pdf online. After many research I have found that,
There are likely two ways to show pdf online in android:
1) In Web view by loading simple hosted URL
2) By google doc.
But in our case, if we use way (1), the mobile web view does not redirect hosted url of PDF.
And if we use way (2), then there are certain limitations of the google docs regarding the file sizes. (Small pdf it will load like 5 to 10 with size of 1MB)
But after loading PDF with the size of 2 or 3 MB it's also giving me error as "No PREVIEW AVAILABLE" and its continuously giving me this error opening different urls.
So it seems there is still no easy solution for viewing a pdf from url (online without downloading pdf).
The other way to open pdf online is that we can open it in any other third party applications like browser, adobe pdf reader or any kind of pdf viewer application available in the device.
Cons: If we open pdf in browser then it will get downloaded automatically and if we open it in any third party application then they have options to download the pdf.
Please give me some solution to read the PDF online.
And if there are any paid pdf SDK availble which gives functionality of loading pdf online from URL, then please provide information of that too.
Please also suggest me some pdf viewer library for displaying pdf online.
I have used many of the library, but it increases my application's size to 8 to 10 MB.
Thanks in advance.
The suggested primary solution,
Download the file, store it in the app specific folder so users don't have access. For viewers who don't have access rights to download it, you will delete the file as they leave the view. For viewers who have access rights to download it, they will be given an option to copy the file to their SD card (an accessible location) and then you will delete the original file as they leave the view.
For storing in app specific directory to restrict user access,
http://www.grokkingandroid.com/how-to-correctly-store-app-specific-files-in-android/
Also use a library to view the pdf(MUCH SIMPLER), choose an appropriate one from here
https://android-arsenal.com/search?q=pdf
Alternate solution,
If security is a major concern, you can encrypt the pdf file and store it on the server. And decrypt the file when you download it to the device.
For added security, don't store the file as pdf, just store it as a file. Download it as a file. Set type as pdf when you want to access it.
Conclusion,
Data wise, Even if you load it online, the device will consume almost same data as downloading the pdf. (Infact for viewing something online, your device downloads the data and stores it in the cache and you can view it)
Security wise, only a rooted phone will be able to access the file but that too for as long as you have decided to store it.
I suggest you check out PDF.js, a Javascript library from Mozilla to render PDF's in a browser. You can adapt this into a WebView easily, and display PDF's without downloading them.
Here is an open source app which does something similar to what you're looking for
I am working on an application where I load few websites in webview now I want to save webpages so after sometime even if there is not internet user will able to see those pages. But I am confused on how to save whole webpage in cache or any other medium. The main thing is we need to show pages back even if there is not internet.
Has anyone implemented this before. Please provide some demo code as this is my first attempt on cache..
Thank You
The easiest way is save webpages in cache directory or any other(Internal or external storage)
You can get the data of web page using HttpClient.execute() or HttpClient.get() now store that data in .html file also you have to download images or other contents which are bind to that page, Now in your application you have to check for connection if connection not available then load the page which one you saved in storage with file://<location of your webpage..>
EDIT:
I think using HTML5 you can display off-line webpages. (I never tried this, but I referred some blogs on it). Look at this nice post about HTML5 Creating mobile Web applications with HTML 5, Part 3: Make mobile Web applications work offline with HTML 5 Also this
hope this will help you.
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