androidkotlinandroid-sharedpreferences

Array of images to store in cache?


I am working with the cards (cardView) and trying to store the images inside the array (array to string; store that in cache and when required unpack back to array). My question is: what is the most convenient way to store the images in an array?

Store the bitmaps (bitmapToString etc)? Turn the bitmaps (I get the images from the gallery and camera) into URIs? Absolute paths?

The number of images is small (3-4), so, I guess, there's no need in a DB.


Solution

  • Android Bitmap objects are not good candidates for storing in Java arrays - they are very large objects so you run the risk of an OutOfMemoryException. It is easy to create a memory leak if you are not careful.

    If you have to get some photos from the gallery as Bitmap and retain them in your app, one of the patterns that is normally used is writing the Bitmap to the app's internal storage (covered here.

    If you need a reference to the stored photo, you can save the filename or a Uri that represents the photo or image.

    Then the problem of loading Bitmaps etc. into an ImageView is solved with libraries like Picasso or Glide. You can use these to load from the local file system, or from online.