Android的序列化问题 [英] Android serializable problem

查看:117
本文介绍了Android的序列化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个类,它有几个成员变量,所有这些都是可序列化......除了一个位图!我试图扩大位图并实现序列化的,没有想到位图是一个final类。

I created a class, which has several member variables, all of which are serializable... except one Bitmap! I tried to extend bitmap and implement serializable, not thinking Bitmap is a final class.

我要保存类(它基本上形成了一个游戏的当前状态),所以玩家可以拾取并加载游戏。

I want to save the class (it basically forms the current state of a game) so a player can pick-up and load the game.

我看到它的方式我有两个选择: 1)找到另一种方式来保存游戏状态。在这里任何帮助将是AP preciated。

The way I see it I have two options: 1) Find another way to save the game state. Any help here would be appreciated.

2)的位图的成员变量更改为int,再说了,创建一个具有静态方法返回基于整数位图一BitmapGetter类。 (此选项是不容易的,因为我的类包含了如此多的位图possiblities和我创建的游戏方式意味着这将需要努力一个令人难以置信的金额。

2) change the bitmap member variable to an int, say, and create a BitmapGetter class that has a static method returning bitmaps based on ints. (This option is not easy, as my class contains so many bitmap possiblities and the way I created the game means this will require an incredible amount of effort.

基本上我有没有人责怪,但我自己懒洋洋地创建不假思索位图变,但我会AP preciate任何帮助......

Basically I have no one to blame but myself for lazily creating a bitmap variable without thinking, but I would appreciate any help...

推荐答案

如何带班替换位图是这样的:

How about replacing Bitmap with a class like this:

public class SerialBitmap implements Serializable {

    public Bitmap bitmap;

    // TODO: Finish this constructor
    SerialBitmap(<some params>) {
        // Take your existing call to BitmapFactory and put it here
        bitmap = BitmapFactory.decodeSomething(<some params>);
    }

    // Converts the Bitmap into a byte array for serialization
    private void writeObject(java.io.ObjectOutputStream out) throws IOException {
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 0, byteStream);
        byte bitmapBytes[] = byteStream.toByteArray();
        out.write(bitmapBytes, 0, bitmapBytes.length);
    }

    // Deserializes a byte array representing the Bitmap and decodes it
    private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        int b;
        while((b = in.read()) != -1)
            byteStream.write(b);
        byte bitmapBytes[] = byteStream.toByteArray();
        bitmap = BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length);
    }
}

被覆盖Serializable.writeObject()和readObject()方法序列化的字节,而不是位图,以便类是可序列化。您将需要完成的构造,因为我不知道你正在构建你的位图。最后要做的是,以取代YourClass.serialBitmap.bitmap引用YourClass.bitmap。

The overridden Serializable.writeObject() and readObject() methods serialize the bytes instead of the Bitmap so the class is serializable. You will need to finish the constructor because I don't know how you currently construct your Bitmap. The last thing to do is to replace references to YourClass.bitmap with YourClass.serialBitmap.bitmap.

祝你好运!

巴里 附:这code编译,但我还没有一个真正的位图测试它

Barry P.S. This code compiles but I haven't tested it with a real bitmap

这篇关于Android的序列化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆