Caching images in android? - android

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.

Related

Internal storage in non-context area

I think it's just a basic question but I used to face the problem from different view. It concerns internal storage or any other method of saving data under android dev.
As far I used to dance with qt cpp/c#/dx9 gaming/etc, but what hit me at android development is that context and disinformation. As usual I wanted to decouple the code a bit, this time I decided to swap fragments in one activity, which got me pain in the ass in form of back button control.
Then I thought to myself, let the model control data retrival and save (viewmodel whatever), and i hit the shit. I've lost the context, so I can't simply get current dir, file locations from .io without passing activity/fragment to model class.
Anyone could tell me what are the methods of retriving data (as dummy content is made, but there is static crap) ? Let it be sync or async, anyway, I want my model to play here and there, what I see is that I hit the wall of missing context because of decoupling. I think I started the wrong way but i can't find the right track.
The main figure here is that I want to use internal storage and sync it with external DB later. The good example of what i'm going to achieve is to render the recycler view and give the app the way to swap it with another view, so i need one clean data model.
Well yeah, I've seen repos as a form of data retrival. I'll retrack the idea and thank you for answer. As we call model (c++ struct) as data only then in fact, data shouldn't load itself, I just wanted to make a shortcut as it's just basic model. On the other end, many web frameworks tend to use models as data factories/loaders, here's my wild concern.

Best way to save history for app

I want to write an android calculator app like the one on my android phone. It saves history for operations and by clicking a button it shows last operations. Now my question is what is the best way to save operations? Is it reasonable to save them to a file in internal storage or what?
There's some options..
1) Include a SQLite Database, as others mentioned. This makes storing lots of information really easy. You can find tutorials on how to include one properly in your project, and don't hvae to care for much more. You can then work with content providers to read and store data.
http://developer.android.com/guide/topics/providers/content-providers.html
2) SharedPreferences. If you just intend to store like the last, or the last 3 Operations, you can just use shared Preferences. This is way less overhead than adding a Database, if it is a small project, albeit you will have to keep your data structured yourself.
http://developer.android.com/reference/android/content/SharedPreferences.html
3) If you just want to store the users current session you can just Keep a Stack of the used operations. On undo, or however you call it, you would just pop the stack.
By implementing onSaveInstanceState and Parcelable you can make sure that no data is lost on rotation / low memory and such.
I personally would advise you without knowing more about your project to use plain java objects and storing the state. A calculater would in most cases not need persistent storage. If you really want to know what the user did 2 weeks ago, you should use a Database.
I would recommend you to use database(SQLite) for storing the data.
If you don't know more about SQLite in android have a look at these
tutorials.
I think database should be handy for history if more than one operations has to be stored else for one operation you can use shared preference.

Storing Bitmap for multiple Activities?

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.

best way to load data from a webservice into a listview

I'm making an adapter that it underline data loads from a web service and each time the user scrolls down it loads more data and adds it to the ArrayList behind that adapter. The data contains image URLs which I then lazy load in my getView method.
Is there a better solution for that?
And how do I properly cache those images?
And can I retain the data of the adapter when the user rotates the device? So no need to load them again from the beginning?
I use fragment to display that list?
I'll try to answer all three questions one by one.
is there a beter solution for that? (other then lazy-loading)
That depends. The nice part about lazy-loading is, that you don't need to have all data right at the start when you display the component. The problem often is, how to get new data, as it is needed.
So, when loading the new data from the network, it is really a question of how big the chunks of loaded data are. I would not load one new entry after the next one, but load e.g. 20 new ones, when the end of the list has been reached.
Of course this is just a random idea for a number, you'll need to find the perfect point between speed and usability for your case.
So when you are loading chunks of data from your web-service, which are delivered fast enough, it really is a cool concept. But if you're just reading one entry after another (and therefore, the user almost always waits for new data), this is really just annoying.
And how to proper cache the images?
There are multiple possible solutions available, some of those are:
Using the system-cache, like the browser (seems to be the
finest).
Implementing your own system which uses device-storage (e.g. the
SD-Card or the internal application storage (the first one is
preferred)).
You might also like the contents of this question (and it's accepted answer), giving you some general advises on memory-management.
can i retain the data of the adapter when the user rotates the device?
Old, deprecated anser:
Yes, you can use the "instance state" of the Activity to do this.
Basically, the onSaveInstanceState()-method from the Activity
allows you to populate a given Bundle with state-information, which
is then passed to the onCreate()-method.
The data you would save in the Bundle would be (for example) the
current position of the list or maybe even the list itself (depending
on the size).
Yes, use the Loaders API. It is also available in the compatibility library.

Good guidelines for developing an ecommerce application [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 months ago.
Improve this question
I'm making an ecommerce application on Android and, as it is my first serious project, I'm trying to figure out beforehand the best approach to do this.
The application talks to a web service (magento api, meaning soap or xml rpc unfortunately) and gets all the content on the phone (product categories, product details, user credentials etc.). I think it should do lazy loading or something like that.
So, I was thinking to keep the user credentials in a custom Object which will be kept in a SharedPreferences so that every Activity can easily access it. I'll use a couple of ListViews to show the content and AsyncTask to fetch the data needed. Should I keep all the data in memory in objects or should I use some sort of cache or a local database? Also, I'm planning to use a HashMap with SoftReferences to hold the bitmaps I am downloading. But wouldn't this eat a lot of memory?
How can all the activities have access to all these objects (ecommerce basket etc.)? I'm thinking of passing them using Intents but this doesn't seem right to me. Can SharedPreferences be used for a lot of objects and are there any concurrency issues?
Any pointers would be really appreciated. What are some good guidelines? What classes should I look into? Do you know of any resources on the Internet for me to check out?
A very detailed question I will do my best to answer it.
I used the following approach in my application:
I save the user credentials in the shared preferences. The preferences can only hold custom objects if they are serializable and writing and reading from flash memory takes a lot of time. Therefore I load the preferences on startup and store them in memory.
I try to keep all the data in memory that is needed in many places and in a consistent state, all the other memory is passed via serializing to json and passing through an intent or i pass only ids and I re fetch it from the net. (There is definitively a possibility for cashing in a local database but the effort to keep it up to date is to much work at the moment.) To store objects that take to long to reload from internal memory or network and re parse it I use a custom application that holds the reference to some controller objects which manage the caching. The application will stay in memory until your app is closed. This is convenient but can result in needing way to much memory if you are not careful.
The bitmaps that are downloaded by my program are cached on two layers. At the first time I want to access an image I create a image manager object inside my activity scope. That object will try to find the bitmap in an internal map. If it is not there it will try to load it from phone memory if it is not in the phone memory it will download it from the net, store it in the cache folder of my app and put it in the map. In this way the bitmap is accessible as long as the activity runs and is cleaned up at the moment the user changes to another screen. Until now this is sufficient for me.
At the end just begin programming and come back if you encounter other questions or errors and post some more specific questions.
Many useful techniques that you will need to use: ContentProviders, AsyncTasks, Bitmap Caching, ... are used in Romain Guys 'Shelvs' (http://www.curious-creature.org/2009/01/19/shelves-an-open-source-android-application/) app. It's a great start point to get the into a recommend Android flow.

Categories

Resources