JnJ11
JnJ11

Reputation: 35

SharedPreference or Map<String, SoftReference<Bitmap>>

I have more than 10 images to be dynamically fetched from server when my app launches (mostly just the 1st time the app launches).

What is the best practice for doing this and why?

  1. Maintain a Map<String, SoftReference<Bitmap>> where I will have a hashmap where the elements are SoftReference.

  2. Saving byte[] using

String encodedImage = Base64.encodeToString(imgData, Base64.DEFAULT);
SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit=shre.edit();
edit.putString("image_data",encodedImage);
edit.commit();

Can someone illustrate the advantage of each?

Upvotes: 1

Views: 97

Answers (1)

Serj Lotutovici
Serj Lotutovici

Reputation: 4380

Actually you should use something like an Image Cache. For example, you can use the UniversalImageLoader library. It will help you to optimize the memory usage of your app.

There are a lot of exapmles in the web, but basicaly everything comes down to one line of code:

imageLoader.displayImage(imageUri, imageView);

Also check out these links:

  1. Displaying Bitmaps Efficiently - from Android Developers site
  2. Picaso Library - from Square, that's where Jake Wharton works.

Upvotes: 1

Related Questions