Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Storing images downloaded from the web

I had a question related to whether or not (and how) I should store images loaded from the web. Let's say I am calling a web service from my Android app. In this web service I get a URL for an image on the web. I download and show this image on the left side of a list item in a ListView. My question is, what method should I use for possibly storing the image? Should I:

  1. Save it to the SDCard, checking if it exists when the ListView is created (on subsequent requests) and re-downloading as necessary (while updating the image occasionally, in case it changes).
  2. Store it in the cache using Context.getCacheDir(), but possibly being forced to re-download it more often since I cannot rely on the image staying in the cache.
  3. Always download it and never store the image.

The image files themselves are fairly small, but I expect some users could have dozens of these small images downloaded/stored. Which would work best, and/or what is the preferred method?

As a side question, should I load all of the images in my ListView first (and possibly lock the UI for some time) or load them asynchronously but show a placeholder graphic in the meantime (which might be a bit more "ugly")? What's the standard here?

like image 268
Bara Avatar asked Jan 13 '10 06:01

Bara


People also ask

How to store images in Android?

This example demonstrates How to write an image file in internal storage in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

Where to store images Android project?

You have to put the images in the res/drawable folder. Then, you can access them by using: R. drawable. name_of_image (for name_of_image.

Where are downloaded images saved from Google?

Default save location for the downloaded image from google is on downloads folder located in Drive C.


1 Answers

About where to store: The answer depends on what is being downloaded and how much. Then you make a choice.

For instance: If you are downloading something that is temporary, less in number(less remote fetches) and size(less memory) and is local to an Activity then you should consider holding the images in memory using SoftReferences. SoftReferences can lead to refetches but since the number of items is small it should be affordable.

However, if the number of items to be downloaded exceeds a certain threshold(meaning more fetches and memory) you should consider reducing the fetches and also the Runtime memory consumption by caching them. Here you can either choose to save them on Sdcard or on temporary storage(cache directories local to app). For items that are small and hold meaning only in the context of Application(e.g thumbnails), the user will mostly not use it outside of your application. So, you can store such things in the cache directory. The best part of using them is that you don't have to clean the mess. It is handled automatically. It might lead to re-fetches though.

However, if the downloaded items are large in size and can stand alone outside the context of the application such as pictures, video, audio clips then SDcard should be your option. You should also read: Handling large Bitmaps to avoid OOM error during BitmapFactory.decodeStream(..)

Note that you can also check to see if using a database can help here.See this

Some considerations while lazy loading items in a ListView: You should do the loading in the background and not block the UI thread.You should consider showing a temporary image while the item is being downloaded. This is apparent in many native applications. For an example implementation of lazy loading see this. Additionally, for large lists, you can implement the SlowAdapter pattern(check API demos). It basically stalls download while the list is scrolling.

Example Projects that can help you here:

Romain Guy's Shelves project uses two level of caching wherein he employs an in-memory cache(HashMap containing SoftReferences) and storage on Sdcard.Browse Source code here

There are also some open source libraries that Mark Murphy wrote(CWAC) and DroidFu that can help here.

Good Luck!

like image 198
Samuh Avatar answered Dec 03 '22 11:12

Samuh