反序列化的Andr​​oid图像的最佳方法

反序列化的Andr​​oid图像的最佳方法

本文介绍了以序列化/反序列化的Andr​​oid图像的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android中坚持的图像。我最终定居在创建一个包装对象来处理序列化。我希望这是次优的。

我的问题:这怎么可能做的更好(尤其是关于性能和多个序列化没有苦难的图像劣化)

 公共类SerializableImage实现Serializable {私有静态最后的serialVersionUID长1L =;私有静态最终诠释NO_IMAGE = -1;私人位图图像;公共位图的getImage(){
    返回形象;
}公共无效setImage(位图图像​​){
    this.image =图像;
}私人无效的writeObject(ObjectOutputStream的了)抛出IOException
    如果(形象!= NULL){
        最后ByteArrayOutputStream流=新ByteArrayOutputStream();
        image.com preSS(Bitmap.Com pressFormat.PNG,100,流);
        最后一个字节[] = imageByteArray stream.toByteArray();
        out.writeInt(imageByteArray.length);
        out.write(imageByteArray);
    }其他{
        out.writeInt(NO_IMAGE);
    }
}私人无效的readObject(ObjectInputStream中的)抛出IOException异常,ClassNotFoundException异常{    最终诠释长度= in.readInt();    如果(长度!= NO_IMAGE){
        最后一个字节[] = imageByteArray新的字节[长度]
        in.readFully(imageByteArray);
        图像= BitmapFactory.de codeByteArray的(imageByteArray,0,长度);
    }
}
}


解决方案

由于PNG是你应该没有质量下降无损格式。你应该留意是不是这么多,你怎么写/来自文件,但多久你做阅读图像/。我发现,使用创建弱引用的内存缓存大大降低了IO要求。另外要注意,即使你让系统垃圾回收这些被缓存在本地code更长的时间,旧的图像。唯一的调用,可以帮助你处理这个呼吁Bitmap.recycle

I'm trying to persist images in Android. I eventually settled on the creating a wrapper object to handle serialization. I expect that it is sub-optimal.

My question: How could it be done better (especially with respect to performance and not suffering image degradation from multiple serializations)?

public class SerializableImage implements Serializable {

private static final long serialVersionUID = 1L;

private static final int NO_IMAGE = -1;

private Bitmap image;

public Bitmap getImage() {
    return image;
}

public void setImage(Bitmap image) {
    this.image = image;
}

private void writeObject(ObjectOutputStream out) throws IOException {
    if (image != null) {
        final ByteArrayOutputStream stream = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.PNG, 100, stream);
        final byte[] imageByteArray = stream.toByteArray();
        out.writeInt(imageByteArray.length);
        out.write(imageByteArray);
    } else {
        out.writeInt(NO_IMAGE);
    }
}

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{

    final int length = in.readInt();

    if (length != NO_IMAGE) {
        final byte[] imageByteArray = new byte[length];
        in.readFully(imageByteArray);
        image = BitmapFactory.decodeByteArray(imageByteArray, 0, length);
    }
}
}
解决方案

Since PNG is lossless format you should have no degradation in quality. What you should watch for is not so much how you write/read image to/from file but how often you do it. I found that creating in-memory cache using weak references greatly reduces IO calls. Also be aware that even if you let system garbage collect the old images these are cached for longer times in the native code. The only call that helps you to deal with this is calling Bitmap.recycle

这篇关于以序列化/反序列化的Andr​​oid图像的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 10:08