android - Why can I pass a serializable with a bitmap through a Bundle but get an error when the system tries to serialize it? -
so have class, image implements serializable:
public class contact implements serializable { ... public static final string contact_key = "contactkey"; private transient bitmap mimage; ... }
i pass fragment:
bundle bundle = new bundle(); final fragmentmanager fragmentmanager = getfragmentmanager(); // load fragment fragment contactfragment = new contactfragment(); contactfragment.setarguments(bundle); fragmentmanager.begintransaction() .replace(r.id.contactcontainer, headerfragment, "contactfragment") .commit();
and pull image out (successfully) , use it:
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()); } } } } } }
but when background app receive runtime exception system:
process: my.package, pid: 11680 java.lang.runtimeexception: parcelable encountered ioexception writing serializable object (name = my.package.contact) @ android.os.parcel.writeserializable(parcel.java:1316) @ android.os.parcel.writevalue(parcel.java:1264) @ android.os.parcel.writearraymapinternal(parcel.java:618) @ android.os.bundle.writetoparcel(bundle.java:1692) @ android.os.parcel.writebundle(parcel.java:636) @ android.app.fragmentstate.writetoparcel(fragment.java:132) @ android.os.parcel.writetypedarray(parcel.java:1133) @ android.app.fragmentmanagerstate.writetoparcel(fragmentmanager.java:373) @ android.os.parcel.writeparcelable(parcel.java:1285) @ android.os.parcel.writevalue(parcel.java:1204) @ android.os.parcel.writearraymapinternal(parcel.java:618) @ android.os.bundle.writetoparcel(bundle.java:1692) @ android.os.parcel.writebundle(parcel.java:636) @ android.app.activitymanagerproxy.activitystopped(activitymanagernative.java:2467) @ android.app.activitythread$stopinfo.run(activitythread.java:3084) @ android.os.handler.handlecallback(handler.java:733) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5001) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:785) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:601) caused by: java.io.notserializableexception: android.graphics.bitmap @ java.io.objectoutputstream.writenewobject(objectoutputstream.java:1364) @ java.io.objectoutputstream.writeobjectinternal(objectoutputstream.java:1671) @ java.io.objectoutputstream.writeobject(objectoutputstream.java:1517) @ java.io.objectoutputstream.writeobject(objectoutputstream.java:1481) @ java.io.objectoutputstream.writefieldvalues(objectoutputstream.java:979) @ java.io.objectoutputstream.defaultwriteobject(objectoutputstream.java:368) @ java.io.objectoutputstream.writehierarchy(objectoutputstream.java:1074) @ java.io.objectoutputstream.writenewobject(objectoutputstream.java:1404) @ java.io.objectoutputstream.writeobjectinternal(objectoutputstream.java:1671) @ java.io.objectoutputstream.writeobject(objectoutputstream.java:1517) @ java.io.objectoutputstream.writeobject(objectoutputstream.java:1481) @ android.os.parcel.writeserializable(parcel.java:1311) @ 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)
presumably tries preserve fragments arguments.
i know shouldn't trying serialize bitmap this, question why work? android doing kind of optimization?
in both cases contact serialized what's going on?
caused by: java.io.notserializableexception: android.graphics.bitmap
ok clarify bitmaps not serializable i.e. can't broken down , saved disk (hard cache).
in first case, using soft cache map maybe, bitmap can saved in memory. (soft cache)
when background app, system tries save data disk has serialize things , thats when crash.
make sense?
Comments
Post a Comment