可以使用 ObjectOutputStream 将位图写入缓存吗?

发布于 2024-10-27 22:08:39 字数 463 浏览 1 评论 0 原文

我有一个名为 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(...) 方法来保存位图,但它们对于我的需求来说有点慢...是的,位图已写入这些位置,但我不知道它是否(位图)是可序列化的,那么它真的可以保存吗?是的,我在写文件时记得刷新。

I have a method called loadFromCache and it returns a bitmap if it is found in the cache. Long story short I have it narrowed down to this method, returning null at the end if the try/catch fails.

FileInputStream fis = new FileInputStream(getCacheDir()+(""+position));
        ObjectInputStream ois = new ObjectInputStream(fis);
        Bitmap temp = (Bitmap)ois.readObject();
        fis.close();
        return temp;

I have previously tried the Bitmap.compress(...) methods to save bitmaps but they were a little slow for my needs... Yes the bitmap has been written to these positions, but I don't know if it (Bitmap) is serializable so is it actually saving? And yes I remembered to flush when I wrote the file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑梦风尘 2024-11-03 22:08:39

Bitmap.compress() 对您来说太慢了?唯一更快(?)的方法是将位图不变地写入磁盘,见下文。

MappedByteBufferBitmap.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 with Bitmap.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.

甲如呢乙后呢 2024-11-03 22:08:39

分享我刚刚遇到的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文