Android:从Parcelable到Serializable的任何快捷方式,而不使用marshall()?

前端之家收集整理的这篇文章主要介绍了Android:从Parcelable到Serializable的任何快捷方式,而不使用marshall()?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我知道Parcelable(快速)和Serializable(慢速)之间的性能差异.但是,我需要持久存储某些应用程序信息,而不仅仅是在一个生命周期内,因此onSaveInstanceState和使用Parcelable对象的相关方法是不合适的.

所以我把注意力转向Serializable.主要是我有要存储的AbstractList类型 – 这很好,因为它们实现了Serializable.但是,我在其中存储的许多类型都是Parcelable但不是Serializable,例如RectF.

我认为“没问题”,因为我可以通过Parcelable.writeToParcel(parcel,flags)轻松生成一个Parcel,然后在其上调用marshall()来创建一个可以序列化和反序列化的byte [].我想我会使用泛型;创建一个SerializableParcelable< Parcelable>实现Serializable类,为我希望序列化的所有Parcelable类型提供一个适合的解决方案.然后我会…将每个RectF存储在ArrayList中的这个包装器中,然后看看列表及其Parcelable内容是否可序列化.

但是,API文档声明marshall()不得用于持久存储:

public final byte[] marshall ()

Returns the raw bytes of the parcel.

The data you retrieve here must not be placed in any kind of persistent storage (on local disk,across a network,etc). For that,you should use standard serialization or another kind of general serialization mechanism. The Parcel marshalled representation is highly optimized for local IPC,and as such does not attempt to maintain compatibility with data created in different versions of the platform.

所以现在我被卡住了.我可以忽略这个警告并遵循我上面概述的路线,或者通过扩展我想要序列化和创建定制序列化方法的每个Parcelable来规避问题,这似乎非常浪费时间和精力.

有没有人知道在不使用marshall()的情况下序列化Parcelable对象的’正确’快捷方式?或者,如果不注意指定的警告,我应该继续吗?也许sqlite数据库是可行的方法,但我不确定并希望得到你的建议.

非常感谢.

解决方法

对于您需要序列化的任何对象,您可以使用objectOutPutStream.通过使用它,您可以将对象写入设备的文件系统.因此,这也可用于保存Parcelable对象.

下面是将对象保存到文件系统的代码.

public static void witeObjectToFile(Context context,Object object,String        filename) {

    ObjectOutputStream objectOut = null;
    FileOutputStream fileOut = null;
    try {
        File file = new File(filename);
        if(!file.exists()){
            file.createNewFile();
        }
        fileOut = new FileOutputStream(file,false);
        objectOut = new ObjectOutputStream(fileOut);
        objectOut.writeObject(object);
        fileOut.getFD().sync();

    } catch (IOException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        e.printStackTrace();
    }finally {
        if (objectOut != null) {
            try {
                objectOut.close();
            } catch (IOException e) {
                // do nowt
            }
        }
        if (fileOut != null) {
            try {
                fileOut.close();
            } catch (IOException e) {
                // do nowt
            }
        }
    }
}`

为了读取Object,请使用ObjectInputStream.找到下面的代码.

public static Object readObjectFromFile(Context context,String filename) {

    ObjectInputStream objectIn = null;
    Object object = null;
    FileInputStream fileIn = null;
    try {
        File file = new File(filename);
        fileIn = new FileInputStream(file);//context.getApplicationContext().openFileInput(filename);
        objectIn = new ObjectInputStream(fileIn);
        object = objectIn.readObject();

    } catch (FileNotFoundException e) {
        // Do nothing
    }catch (NullPointerException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }finally {
        if (objectIn != null) {
            try {
                objectIn.close();
            } catch (IOException e) {
                // do nowt
            }
        }
        if(fileIn != null){
            try {
                fileIn.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    return object;
}`

问候,沙

原文链接:https://www.f2er.com/android/314335.html

猜你在找的Android相关文章