Happy Holidays everyone! I know peoples' brain are switched off right now, but can you please give me solutions or best practices to cache images in java or j2me. I am trying to load images from a server (input stream) and I want to be able to store them so I don't have to retrieve them from the server all the time if they need to be displayed again. Thank you.
3 Answers
The approach you'll want probably depends on the number of images as well as their typical file size. For instance, if you're only likely to use a small number of images or small-sized images, the example provided by trashgod makes a lot of sense.
If you're going to be downloading a very large number of images, or images with very large file sizes, you may consider caching the images to disk first. Then, your application could load and later dispose the images as needed to minimize memory usage. This is the kind of approach used by web browsers.

- 18,685
- 15
- 71
- 81
The simplest approach is to use whatever collection is appropriate to your application and funnel all image access though a method that checks the cache first. In this example, all access is via an image's index, so getImage()
manipulates a cache
of type List<ImageIcon>
. A Map<String, ImageIcon>
would be a straightforward alternative.

- 203,806
- 29
- 246
- 1,045
-
thanks for the solution. might think of using map. don't know why, but collection tend to confuse me. :( – irobotxx Dec 28 '10 at 01:57
-
@manuelJ: Sorry about the bogus collection reference; I've updated the link. – trashgod Dec 28 '10 at 02:23
The only way to do this in J2ME is to save the images' raw byte array (i.e. that you pass to Image.createImage()) to somewhere persistent, possibly a file using JSR75 but more likely a record store using RMS.
HTH

- 8,432
- 6
- 39
- 52
-
Thanks for the input. i thought of using record store, but i wanted to know if there were other better solutions – irobotxx Dec 28 '10 at 01:54