所以我有一个类,一个实现可序列化的图像:
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正在进行某种优化吗?
在这两种情况下,联系人都会序列化,以便进行什么?
答案 0 :(得分:2)
Caused by: java.io.NotSerializableException: android.graphics.Bitmap
好的,所以澄清位图是不可序列化的,即它们不能被分解并保存到磁盘(硬缓存)。
在第一种情况下,它将使用像Map一样的软缓存,因此位图可以保存在内存中。 (软缓存)
当您对应用程序进行后台处理时,系统会尝试将您的数据保存到磁盘,以便在发生崩溃时必须将事物序列化。
有意义吗?
答案 1 :(得分:0)
由于 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);
}
适合我;)