开发者

Can Bitmaps be written to cache using ObjectOutputStream?

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, bu开发者_运维知识库t 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜