可以使用 ObjectOutputStream 将位图写入缓存吗?
我有一个名为 loadFromCache 的方法,如果在缓存中找到它,它会返回一个位图。长话短说,我将其范围缩小到这个方法,如果 try/catch 失败,则最后返回 null。
FileInputStream fis = new FileInputStream(getCacheDir()+(""+position));
ObjectInputStream ois = new ObjectInputStream(fis);
Bitmap temp = (Bitmap)ois.readObject();
fis.close();
return temp;
我之前尝试过 Bitmap.compress(...) 方法来保存位图,但它们对于我的需求来说有点慢...是的,位图已写入这些位置,但我不知道它是否(位图)是可序列化的,那么它真的可以保存吗?是的,我在写文件时记得刷新。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Bitmap.compress()
对您来说太慢了?唯一更快(?)的方法是将位图不变地写入磁盘,见下文。将
MappedByteBuffer
与Bitmap.copyPixelsToBuffer()
< /a> 可能有效。我还没有测试过这个,但看起来它可以工作。请注意,您很可能必须自己存储图像尺寸。How is
Bitmap.compress()
too slow for you? The only faster(?) way would be to write the bitmap unchanged to disk, see below.Using
MappedByteBuffer
together withBitmap.copyPixelsToBuffer()
may work. I haven't tested this but it seems like it could work. Note that you most likely will have to store image dimensions yourself.分享我刚刚遇到的 Bitmap.compress 速度非常慢的经历:
我的源图像是 jpeg,我将 Bitmap.compressFormat.PNG 传递给 bitmap.compress。这导致压缩操作需要 10-15 秒。
一旦我将其更改为 JPEG(使得源文件和目标文件保持相同的图像格式),则操作只需不到一秒。也许最初的问题是通过类似的方式提出的,也许其他人发现这很有帮助。
Sharing an experience I just had with Bitmap.compress being very slow:
My source image was a jpeg, and I was passing Bitmap.CompressFormat.PNG to bitmap.compress. This caused the compress operation to take 10-15 seconds.
Once I changed it to JPEG (such that the source and destination file remain the same image format) then the operation takes less than a second. Perhaps the original question came about through a similar means, and maybe someone else finds this helpful.