Loading website from Internet, and data from local files - android

I have a website that I'd like to load in WebView. However I have also files (such as images, *css, *js and so on) in assets folder that are needed to open that website. Is it possible to, while loading my website, use as much data from assets as possible and download all the rest from Internet in order to make my site load faster?
For example let's assume that my website needs two image files for proper looking: A and B. I have the A file in assets, but I haven't B at all. Is it possible to make WebView display website with A file from assets folder and B fetched from Internet?

It does depend on the complexity of your website, but assuming it's capable of running offline…
You could download the online content and store it locally in the same folder as the offline content. Then open it. So long as all the references in any downloaded script files are relative you shouldn't have a problem.
You might need to copy the local assets content from a read-only location to a read-write-location, but you could do that on first running the app.

Related

using html as front end and text file as back end can save the data in android

My requirement is doing a project with android, now I developed the project in asp.net and call the URL in android web view. It works fine with the internet connection, but when internet is not available the problem occurs, what I have to do?
using html as front end and text file as back-end can save the data and sync when internet connection available, Is it possible?
or any other easy method to do?
please provide some suggestion.
Yes. You can do what you said. You could have a local copy of the web inside the app assets folder and, for example, you can load it like this:
webView.loadUrl("file:///android_asset/web/index.html");
Then you will have to implement an automatic downloader for the new online version. You could do it by putting a simple txt file with the current version (that you have to compare with the one in the app assets) or you could do a restapi that also handles the download (that's the better way, but it needs more time obviously).
Also, you have to write the files you download (for the new version) to the app internal memory (you could get the folder with "context.getFilesDir()") and then check if there is a version of the webapp inside that folder and if not fall back to the one in assets directory (that's included with the app apk).

How to provide some resource files for an android application?

I'm writing an android application, which user can download some image files from server. There image files will be stored in android mobile.
Now I want to put some of image files inside the apk file, that user can start the application quickly after installing. I found I can put them into assets directory, but the directory is read only. When user download other image files, I need to store them into another directory.
So there will be two directories to store the image files, but they are the same type.
Is there any good solution for this case?
Check out http://developer.android.com/guide/topics/data/data-storage.html#filesInternal for a listing of different places you can put data on Android.
You should put downloaded image files into one of three places, depending on your needs.
If the images are meant to be viewable by the user (e.g. downloaded photos), put them on the external storage. If they are meant to be user-interface elements or other crucial (but not user-facing) images, put them on internal storage. If they are meant to be cached for quick access but downloaded if necessary (e.g. temporary images like those found on a website), put them in the internal cache directory (Context.getCacheDir()).
If you don't have a lot of assets, you can copy them to the target location when your program first runs (e.g. check for the existence of a certain file, and create that file when you are done setting up). Then you only have to check one place (unless it's the cache dir, in which case you can't guarantee that the files will stick around forever).
If you have a lot of asset files, I would use a two-stage lookup: consult your downloaded image directory first (so you can override builtin assets, for example), then consult your assets directory. This is also flexible enough to allow you to make use of multiple storage locations should you find the need.

What are the file formats in Android WebView cache directory (data_1, f_000001 etc.)?

I created a rudimentary browser with help of a WebView.
When I visit a website (containing some text and a few images), the cache directory in /data/data/com.mayexample/cache/webViewCacheChromium gets filled with a few files called index, data_0, data_1, data_2, f_00001, f_00002 etc.
I was wondering, what's the format of these files, what do they contain? I thought about "so, a few of these files surely have to be the website's images then" and tried opening them in a file manager (open as image). But whatever file I pick, the process says "Failed loading!". Even if I rename some of them to .jpg, still I can't open anything.
I have read on the internet that this worked for some people though (look at Android WebView - Load Images from cache, it's exactly what I want to do), but I can't do anything with the cache's files.
Do you know a way to open webview cache files? Doesn't one file represent a corresponding cached image for example?
What I really want to achieve (once I understand the structure of the cache files) is to programmatically fetch images of the webview from it's cache, like the author of the link posted above (unfortunately this posting's answers don't help much)
Thanks!
The cached files might be one of the CSS, or JS/image/html types. On earlier Android releases, one cached file maps exactly one CSS or JS/image/html file.
As far as I know the Browser engine (actually the HTTP module) maintains such cached files in a URL-to-HashKey manner. So what you found (such as "4f42185de3a3a461_1") may be associated with any web resource files such as JS/CSS/images/HTML, etc.
I remember WebView used to store such URL-to-HashKey mapping data in sqlite3 tables in earlier Android versions.
The problem here is you have no idea about the mapping relations so you can hardly retrieve the file you want. A tricky way is to read the AOSP source code then you may be able to know how the generate the HashKey by an unique URL, or you can manipulate the sqlite3 tables, if there are still any on Android 4.4.
I was able to view files in the webViewCacheChromium folder using this tool designed for the Chrome desktop browser:
http://www.nirsoft.net/utils/chrome_cache_view.html
The contents are basically what you'd expect for web cache - images, javascript, css, html.

Storing lots of small web pages on Android

I'm developing an application that uses about 150 small HTML pages (up to 25-30kb, actively used by WebView).
My question is how to save this kind of data on Android device ?
Maybe XML or SQLite......
Thanks
Put all the pages in Assets folder. They will be packed together in the apk and access them as below:
webview.loadUrl("file:///android_asset/your_html_page.html");
You can write them at runtime into a specific directory in the sdcard (if present) and load them from it when you want...

Loading files from web

I have the following situation:
I have files under raw directory. I use them to load them to textview.
I want user to load files from web to be used the same way.
Is it possible? Or do I need to load them to SD?
If so - on the SD - how do I prevent reading\copy of these files (in raw directory it is not reachable).
You can load files to the SD card, to internal storage, an SQLite data base, or as a Preferences object. This is discussed here, including security aspects.
What you cannot do is dynamically add to the resources or assets folder that ships with your app.

Categories

Resources