I'm looking for a way to store some Bitmaps that can be accessed by multiple activities, without having to be reloaded from the web.
I do NOT want to simply pass them from Activity to Activity by putting them into an Intent, but rather have one place where I can access them without having to pass them.
I looked into caching to help solve this problem, but if this is the solution I'm a little unclear as to how to make the cache accesible in multiple activities.
Any suggestions or alternative solutions would be greatly appreciated.
You can either use LruCache, a library such as Volley, or implement the same functionality yourself. I think that the volley library would be perfect for you.
To access it from anywhere in your application you should either store it in a special application object or in a static variable. Note that only a reference to the cache will be stored there. The size of the cache is in both cases listed above configurable.
What does the cache accesible in multiple activities means ? Just dowload it from web, save to SD card (chache it) and then load it from SD card from as many Activities as you want.
Here are some example of how you can store data in Android.
You can use Picasso loader for this task.
You can make static array or map of bitmaps and access it from where u want.
If there are a lot of images you can also make queue and store only most recent used there.
Related
I have a list of about 88,000 surnames,
I need to read it every few minutes, so instead I read it once and store it in a list.
It that better or should I read every time.
Memory vs Run-Time
It that better or should I read every time
Neither, IMHO. Use a smarter cache.
Have a static thread-safe cache for this data.
Design the API around that cache such that the cache can be lazy-loaded. Whoever is seeking to examine the cache should get the results asynchronously, so if the cache is empty, you can load the cache contents on a background thread. This could be accomplished using a listener/callback pattern, RxJava/RxAndroid, LiveData, etc.
Have a custom subclass of Application, and in there, override onTrimMemory(). If onTrimMemory() is called with a value of TRIM_MEMORY_BACKGROUND or higher, empty the cache, so that your memory usage is lower when the user is not actively using your app.
This way, the code using the cache does not care whether the data needs to be loaded or not, and you can minimize memory consumption when it is unlikely that the cache needs to be used.
Or, if you never need all 88,000 surnames in memory at once, store them in a SQLite database and query that database as needed.
In general run-time uses memory so you should be on same. Difference is that you can store in memory as an object and remove it from memory as you need so some memory picks will be present instead of consistent memory load.
Did you consider using API or JavaScript for that logic and load JSON instead? Approach with different architecture using API or JavaScript as business logic to load JSON might help with loading and unloading object that contains data. JSON can be static stored locally or even dynamic (stored in remote server) which can help with easy update of your surnames. Additionally, this approach can query data as per parameters passed so you might need not to load all 80.0000 surnames at once. I see that question is about Android but architecture approach could be the same for any device.
I hope this might help you or somebody else to determine approach depending on architecture and mobile platform (is not really defined in question if any is used).
Cheers!
Currently, I am using a WeakHashMap to store my BitMap and link it to my key, but I have noticed it begins to use a ton of RAM. Is there a better solution to caching the BitMaps till I need them? It is unlikely I will need the image after the application closes, so a long-term solution isn't really needed.
Thanks!
I need to keep them in memory in case the user swipes back to a
previous view to save bandwidth and make the ViewPager more responsive
FragmentStateAdapter is meant for precisely that. Also use setOffscreenPageLimit() and setRetainInstance() in conjunction with that.
Storing Bitmap objects in runtime memory is not a good idea and will sooner or later lead to OutOfMemoryErrors getting thrown.
It is unlikely I will need the image after the application closes, so a long-term solution isn't really needed.
Storing images in cache wont be a problem as system doesn't clear image from cache even after you close the application or unless you clear the app data from setting.
For image downloading you can use image libraries like:
Image loaders:
https://github.com/nostra13/Android-Universal-Image-Loader
It gives you option to save image in cache.
You can also use volley library. But in this you have to implement your own code for cache storing.
http://developer.android.com/training/volley/index.html
Otherwise you can also implement your own code for cache in which you can use memory disk for caching.
http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html#disk-cache
Use an LruCache. There are plenty of online guides on the developer site.
I'm terribly new to Android, and what I want to do, is to save 2 arraylists permanently. The context is as follows: The user will select an option from a listview, and the app will load the 2 arraylists with their corresponding data, according to which option he chooses. I've explored quite a few options, such as saving with SharedPreferences, but I'm not exactly sure how to implement the methods discussed. Will provide code if required!
This link provides information about all storages, usable in your context. Code examples are provided as well.
I don't think that SharedPreferences are what you are looking for. I would rather use a database (might be oversized for your use case) or simply save a file to the filesystem (also described in the link).
If you choose the second approach, this link will help you how to serialize your object (class containing both lists)
The easiest is to use java Serialisation, which you write into a file.
If the lists are huge (thousans elems) then could write using custom Serialisation by using DataOutputStream. This uses less space and is faster, but more work.
I'm currently building an android application, and I'm making some HttpRequests to grab images from an API. Right now I'm just storing them in an object container, and then storing them in ArrayLists, but I want to store it into a temporary cache for the application so that when I quit out of that particular activity and go back into the launcher activity, when I go back into that activity, I won't have to make another httprequest for the image.
However, I don't know where to start, what to read up on, or anything regarding temporary storage. I've only used SharedPreferences, and passing extras along intents. Can anyone point me to any good places to get started with, either documentation or sample code?
edit: I forgot to mention that I'd like the data to be deleted when I quit out of the application. I'm not too sure what caching even means, so I don't know if this happens by default when people talk about "caching"
You could refer to Android - How do I do a lazy load of images in ListView.
There are a cache example in Fedor's LazyList.zip from the answers.
The Activity class has some great information on cache directories and the Lifecycle. If your cache is more than a couple megabytes you should consider using external storage.
I am making an android app which is going to need a lot of pictures. But I can't have them all in the app since it would be like 10 gigabytes or something. I have read some other questions where they say its smarter to store the url to the pictures in a database. My question is, should the image url be stored in the application itself or in the database?
The DB is a part of the application itself. Is it not? If you are talking about hard-coding , keeping them in DB is better keeping extensibility in mind but would involve extra call to DB.
Hard-coding them would be better performing (How much, you need to test that).
Alternatively you can make use of build tools by storing the urls in a configuration and replacing them at the time of build. While this solves above two problems, It adds complexity to the code.
So everything has its gives and takes, you need to decide what to chose on your requirements and priorities.
If you store the URLs in a database, you are able to manage your images easier, as you don't have to hardcode everything.
store URL in a DB will more preferable. And you can load the images using picasso library would better easier and comfortable way.
Its always better to store image url's in a database, whether that database resides on your phone, or on the server and you fetch it from there using rest services is upto you.
You can use multiple libraries for image lazy loading in your app.
[1]: https://github.com/nostra13/Android-Universal-Image-Loader "Universal Image Loader" is by far a very good third party library.
You can also add your own lazy loading algorithms by creating a basic Thread Pool.