2

所以我有一个类,有一个实现可序列化的图像:

public class Contact implements Serializable
{
    ...
    public static final String CONTACT_KEY = "contactKey";
    private transient Bitmap mImage;
    ...
}

我将它传递给一个片段:

Bundle bundle = new Bundle();
final FragmentManager fragmentManager = getFragmentManager();

// Load the fragment
Fragment contactFragment = new ContactFragment();
contactFragment.setArguments(bundle);
fragmentManager.beginTransaction()
            .replace(R.id.contactContainer, headerFragment, "ContactFragment")
            .commit();

并拉出图像(成功)并使用它:

public class ContactFragment extends Fragment
{
    private static final String TAG = "ContactFragment";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        Bundle arguments = getArguments();
        if (arguments != null)
        {
            Contact contact = (Contact) arguments.getSerializable(Contact.CONTACT_KEY);
            ViewGroup view = (ViewGroup)inflater.inflate(R.layout.contact, container, false);
            if (view != null && contact != null)
            {
                ImageView imageView = (ImageView) view.findViewById(R.id.contactImage);
                if (imageView != null)
                {
                    if (contact.hasImage())
                    {
                        imageView.setImageBitmap(contact.image());
                    }
                }
            }
        }
    }
}

但是当我后台我的应用程序时,我收到来自系统的运行时异常:

Process: my.package, PID: 11680
    java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = my.package.Contact)
            at android.os.Parcel.writeSerializable(Parcel.java:1316)
            at android.os.Parcel.writeValue(Parcel.java:1264)
            at android.os.Parcel.writeArrayMapInternal(Parcel.java:618)
            at android.os.Bundle.writeToParcel(Bundle.java:1692)
            at android.os.Parcel.writeBundle(Parcel.java:636)
            at android.app.FragmentState.writeToParcel(Fragment.java:132)
            at android.os.Parcel.writeTypedArray(Parcel.java:1133)
            at android.app.FragmentManagerState.writeToParcel(FragmentManager.java:373)
            at android.os.Parcel.writeParcelable(Parcel.java:1285)
            at android.os.Parcel.writeValue(Parcel.java:1204)
            at android.os.Parcel.writeArrayMapInternal(Parcel.java:618)
            at android.os.Bundle.writeToParcel(Bundle.java:1692)
            at android.os.Parcel.writeBundle(Parcel.java:636)
            at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:2467)
            at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3084)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
     Caused by: java.io.NotSerializableException: android.graphics.Bitmap
            at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
            at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
            at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
            at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
            at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
            at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
            at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
            at android.os.Parcel.writeSerializable(Parcel.java:1311)
            at android.os.Parcel.writeValue(Parcel.java:1264)
            at android.os.Parcel.writeArrayMapInternal(Parcel.java:618)
            at android.os.Bundle.writeToParcel(Bundle.java:1692)
            at android.os.Parcel.writeBundle(Parcel.java:636)
            at android.app.FragmentState.writeToParcel(Fragment.java:132)
            at android.os.Parcel.writeTypedArray(Parcel.java:1133)
            at android.app.FragmentManagerState.writeToParcel(FragmentManager.java:373)
            at android.os.Parcel.writeParcelable(Parcel.java:1285)
            at android.os.Parcel.writeValue(Parcel.java:1204)
            at android.os.Parcel.writeArrayMapInternal(Parcel.java:618)
            at android.os.Bundle.writeToParcel(Bundle.java:1692)
            at android.os.Parcel.writeBundle(Parcel.java:636)
            at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:2467)

大概是因为它试图保留我的片段参数。

我知道我不应该尝试像这样序列化位图,但我的问题是为什么会这样?android是否在做某种优化?

在这两种情况下,联系人都是序列化的,那么发生了什么?

4

2 回答 2

2

Caused by: java.io.NotSerializableException: android.graphics.Bitmap

好的,为了澄清位图不可序列化,即它们不能被分解并保存到磁盘(硬缓存)。

在第一种情况下,它可能会使用像 Map 这样的软缓存,因此可以将位图保存在内存中。(软缓存)

当您在后台运行您的应用程序时,系统会尝试将您的数据保存到磁盘,因此它必须对这些内容进行序列化,这就是您遇到崩溃时的情况。

有道理?

于 2014-10-07T11:55:30.543 回答
1

由于android.graphics.Bitmap对象实现Parcelable了一个可以:

private static final String PARAM_BITMAP = "BLUB";

public static MyFragment newInstance(Bitmap bm) {
    MyFragment frg = new MyFragment();
    Bundle bundle = new Bundle();
    bundle.putParcelable(PARAM_BITMAP, bm);
    frg.setArguments(bundle);
    return frg;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bitmap bitmap = getArguments().getParcelable(PARAM_BITMAP);
}

对我来说很好;)

于 2017-05-20T00:26:03.550 回答