I need to copy all files from assets folder to android cache to load this data faster. The main reason is loading it once during the start of the app. In the whole app lifecycle, I could access needed files from the cache which would be probably faster. I was searching on the Internet but found nothing. How to do that?
-
What, exactly, do you mean by "android cache"? – CommonsWare Aug 07 '17 at 16:06
-
Cache directory, the place in device where files are accessed in the fastest way – shurrok Aug 07 '17 at 16:07
-
What makes you think that a cache directory is "the place in device where files are accessed in the fastest way"? What would a cache directory have to do with "loading it once during the start of the app"? – CommonsWare Aug 07 '17 at 16:09
-
Well, I have got a few `WebView`'s in my app. I use `loadDataWithBaserURL()` to load my HTML. I need to load it faster so I came up with idea to put a cache directory as the first argument to this method (because all JS is read from location given) – shurrok Aug 07 '17 at 16:18
-
@CommonsWare from Wikipedia " A cache is a smaller, faster memory". Does it work differently in `Android` then? I would use those scripts very frequently because my WebView is dynamically changing. – shurrok Aug 07 '17 at 16:22
-
"I need to load it faster" -- when you did the performance analysis to determine where your time is being spent, what did you learn? "from Wikipedia " A cache is a smaller, faster memory"" -- that Wikipedia page needs some editing, if that is what you learned from that page. The term "cache" has many meanings, even within the realm of computer programming. A cache is not some magic form of performance improvement. – CommonsWare Aug 07 '17 at 16:26
-
Ok, I understand. Could you please give me an advice how can I improve loading scripts to HTML then? That was my last idea – shurrok Aug 07 '17 at 16:33
-
"Could you please give me an advice how can I improve loading scripts to HTML then?" -- when you did the performance analysis to determine where your time is being spent, what did you learn? For example, when you measured the time it took you to load the asset into a variable (for use with `loadDataWithBaseURL()`), what did you learn? – CommonsWare Aug 07 '17 at 16:37
-
I didn't measure time. I just see it is done very slowly. – shurrok Aug 07 '17 at 16:40
-
1Then focus on determining *where your time is being spent*, rather than guessing. – CommonsWare Aug 07 '17 at 16:42
-
1@CommonsWare Can't you just point me in the right direction, where to look, what to read or something? I don't want you to tell me exactly what to do... I just want an advice, have no idea what you mean by "determine where your time is being spent" – shurrok Aug 07 '17 at 16:45
1 Answers
One Android "cache directory" is obtained by calling getCacheDir()
on some Context
, such as an IntentService
or JobIntentService
. Copying a bunch of content out of assets into files on the filesystem will take some time, and so a Service
with a background thread may be appropriate. Doing the actual copying is a matter of:
- Getting an
InputStream
on the desired asset fromAssetManager
- Getting a
FileOutputStream
on where you want to write the content to, such as a file inside ofgetCacheDir()
- Using standard Java I/O to copy the bytes
However, please understand that "cache" is not some magic pixie dust that you spread over an app to make it faster. For example, getCacheDir()
is not faster than getFilesDir()
, or getExternalCacheDir()
, or getExtenalFilesDir()
, because they all point to the same hardware (on most devices). Files on the filesystem may be faster to access than are assets, since assets are stored in the APK and require a bit of work to read them out of the APK. So, this may help a bit.
However, since you have not used method tracing, or Log
statements, or anything to determine where your time is being spent, it is entirely possible that you will go through this work and get no net improvement. For example, my main book is published as an APK, among other formats. That book has 200+ chapters, all stored as HTML in assets. I do not find that loading that HTML is especially slow. It is possible that using files rather than assets will help you more than it might help me, due to the nature of what you are doing in those pages.
But it is also possible that your performance issues come from:
- JavaScript doing too much work
- Forgetting that you have a bunch of things that you are downloading from the network, because the URLs to them are buried somewhere (e.g., images referenced in CSS files), and it takes a while for those images to download
- Something else that you are doing in your app, while simultaneously you are trying to load this Web content, and so you are overloading the CPU of the device
- And so on

- 986,068
- 189
- 2,389
- 2,491
-
I have an special class for doing the "cache things". I did the things with the `InputStream` and `getCacheDir`but the assets where not copied. I used the solution from here : https://stackoverflow.com/questions/22903540/android-copy-files-from-assets-to-data-data-folder but it seems not to work properly – shurrok Aug 07 '17 at 17:04