Loading large images as thumbnails without memory issues in Java?

后端 未结 2 1278
挽巷
挽巷 2020-12-09 11:49

I\'m trying to let the user load images from their harddrive, and present these visually in the GUI as a list of thumbnails (JPanels with icons added to a JList). I\'m curre

2条回答
  •  清歌不尽
    2020-12-09 12:35

    The JVM has a nasty habit of caching images. One way to get around it is to:

    1. Create an InputStream pointing to the image.
    2. Read the entire image data into a byte[] (using standard I/O APIs - outside imageio etc.).
    3. Create a ByteArrayInputStream from the byte[].
    4. Use the ByteArrayInputStream as a source for ImageIO.read(InputStream).

    Because the JVM does not know from what resource the image bytes were obtained it is unable to uniquely identify the image, and will not cache it.


    Mind you, I cannot find any documentation that backs up what I am saying. This is just from past experience.

提交回复
热议问题