Canvas: trying to use a recycled bitmap android - onSaveInstanceState -
i'm using camera via intent snap image , save imageview. intent camera in landscape mode , activity returns in portrait mode. activity changes orientation , reloads new activty. i'm trying save image in imageview. while returning activity page disappears once activity in portrait mode. when added onsaveinstancestate method crashes error:
canvas: trying use recycled bitmap android
i have added code below:
public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main_page); imageview = (imageview) findviewbyid(r.id.result); if(savedinstancestate != null){ bitmap photo = savedinstancestate.getparcelable("savedimage"); imageview.setimagebitmap(photo); } }
i have saved image follows
protected void onsaveinstancestate(bundle icicle){ super.onsaveinstancestate(icicle); imageview.builddrawingcache(); parcelable bm = imageview.getdrawingcache(); icicle.putparcelable("savedimage",bm); }
the canvas error because bitmap isn't there more after orientation change. also, parcelable you're setting in onsavedinstancestate() may not there. think of way: views logically "higher level" intents , called-back results.
you can save image file system camera before populate view it.
so, more housekeeping. in several production / released apps ... , there might better way (?)
- define file (path & name) ahead of time before firing camera intent.
- fire camera, grab bitmap image via intent->data parm passed onactivityresult()
- still in onactivityresult, plop bitmap view ,
- then write known file (and close it).
note between step 2 & 3, compress bitmap save space.
to deal reorientation, have routine in onstart or onresume - fired after oncreate():
- try open known file
if fails or has no data:
then use default image (could res jpg),
else file , image good: put image file imageview
close file.
this way, you've got something put in there, no matter happens. of time, works expected :)) ( says after breaking 4 people's wrists flipping android phones @ high speeds lol )
btw - problem can further compounded because camera hardware differs across vendors. devices stay oriented same way before, during, , after camera processing. others, (or ?) newer samsungs, flip camera orientation no matter (unless drop hardware - way way more complex).
is device you're having problems samsung ? - aware can cause more headaches.
Comments
Post a Comment