android - Restore image to ImageView on Orientation Changed -
i writing simple app set image imageview
through intent
calling camera. when change orientation image disappears. i've tried questions q1, q2. previous questions suggest either use onsaveinstancestate
or retaining object during configuration change.
but in android docs i've read:
you might encounter situation in restarting application , restoring significant amounts of data can costly , create poor user experience
so, if bitmap
consider “significant amount of data” need use setretaininstance
, in docs says:
while can store object, should never pass object tied activity, such drawable, adapter, view or other object that's associated context
what approach should use able restore image on orientation change?
what i've done far
private imageview mphotoimgage; private bitmap mcameradata; oncreate { mphotoimgage = (imageview) findviewbyid(r.id.photoresultimage); if (savedinstancestate != null){ mphotoimgage.setimagebitmap((bitmap)savedinstancestate.getparcelable(image_resource)); } } onresume{ if (mcameradata != null){ mphotoimgage.setimagebitmap(mcameradata); } super.onresume(); } onsaveinstancestate{ outstate.putparcelable(image_resource, mcameradata); super.onsaveinstancestate(outstate); }
but after 2 orientation changes image disappears.
you instead ask camera save captured image file external storage (if available). tell save image, , means can store file uri in onsaveinstancestate
, , load uri when you're restoring state.
intent cameraintent = new intent(mediastore.action_image_capture); cameraintent.putextra(mediastore.extra_output, uri.fromfile(new file(context.getexternalfilesdir(null), "randomfilename"))); startactivityforresult(cameraintent, rc_camera);
you should keep track of when file no longer needed , delete it.
another advantage of approach can find out dimensions of image before it's been loaded memory.
Comments
Post a Comment