Java,将对象转换为软引用
作者:互联网
我需要将数据对象放入包含软引用的weakhashmap中.如何将“可绘制”对象转换为软引用?
WeakHashMap <String, SoftReference<Drawable>> tempPulled = new WeakHashMap<String, SoftReference<Drawable>>();
Drawable pulled = BitmapDrawable.createFromResourceStream(null, null, conn.getInputStream(), "galleryImage", options);
SoftReference<Drawable> sPulled;
tempPulled.put(id, pulled);
tempPulled应该放入“ sPulled”(软引用)
解决方法:
我想这就是你要找的
SoftReference<Drawable> sPulled = new SoftReference<Drawable>(pulled);
但是,您可能想添加一个队列,以便在删除引用的对象时从映射中删除SoftRefence(否则您的缓存将随着键和空的softrefence而增长.
标签:soft-references,weakhashmap,memory-management,java 来源: https://codeday.me/bug/20191202/2084998.html