android - ListView Shows Recent Image taken from camera -
in code when take image camera shows correctly in imageview. when take second image, both listview items shows same picture..old picture replaces new one..when take third picture of 3 items show same result.and on..please can solve problem.
public class customerregistrationl0 extends activity { int take_image; int uploadfile; static simpleadapter adapter; static bitmap thumbnail; static string encodedimagestring; bitmap image2; layoutinflater minflater; uri selectedimage ; static listview listviewattachment; public arraylist<listitem> myitems = new arraylist<listitem>(); private myadapter myadapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_cr_l0); //declare fields final edittext textcnic=(edittext)findviewbyid(r.id.edittextcnic); final string cnic=textcnic.gettext().tostring(); final edittext textmobile=(edittext)findviewbyid(r.id.edittextmob); final string mobileno=textmobile.gettext().tostring(); final edittext textname=(edittext)findviewbyid(r.id.edittextname); final string name=textname.gettext().tostring(); final edittext textaddress=(edittext)findviewbyid(r.id.edittextadd); final string address=textaddress.gettext().tostring(); final edittext textkin=(edittext)findviewbyid(r.id.edittextkin); final string nextkin=textkin.gettext().tostring(); listviewattachment=(listview)findviewbyid(r.id.listview1); ////////////////////////////////////////////////////////// //make listview scrollable manuallly(shit) listviewattachment.setontouchlistener(new listview.ontouchlistener() { @override public boolean ontouch(view v, motionevent arg1) { // todo auto-generated method stub int action = arg1.getaction(); switch (action) { case motionevent.action_down: // disallow scrollview intercept touch events. v.getparent().requestdisallowintercepttouchevent(true); break; case motionevent.action_up: // allow scrollview intercept touch events. v.getparent().requestdisallowintercepttouchevent(false); break; } // handle listview touch events. v.ontouchevent(arg1); return true; } }); //////////////// // ////////////////////////////////////////////////////////////// button buttoncamera=(button)findviewbyid(r.id.buttoncamera); button buttonfromgallery=(button)findviewbyid(r.id.buttonattach); button formsubmit=(button)findviewbyid(r.id.buttonsubmit); buttoncamera.setonclicklistener(new onclicklistener(){ @override public void onclick(view view) { intent = new intent( android.provider.mediastore.action_image_capture); startactivityforresult(i, take_image); } }); buttonfromgallery.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { intent intent = new intent(intent.action_get_content); intent.settype("*/*"); intent.addcategory(intent.category_openable); startactivityforresult( intent.createchooser(intent, "select file upload"), uploadfile); } }); formsubmit.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { validate(textcnic,textmobile,textname,textaddress,textkin); //decoding bytes string attachedimage=encodedimagestring; jsonobject jsonobj = new jsonobject(); try { jsonobj.put("cnic number", cnic); jsonobj.put("mobile number", mobileno); jsonobj.put("name", name); jsonobj.put("address", address); jsonobj.put("next kin", nextkin); jsonobj.put("image",attachedimage); string jsonstring=jsonobj.tostring(); file sdcard = environment.getexternalstoragedirectory(); try { file myfile = new file(sdcard,"jsoncache.json"); myfile.createnewfile(); writer myoutwriter = new bufferedwriter(new filewriter(myfile)); myoutwriter.write(jsonstring); myoutwriter.close(); toast.maketext(v.getcontext(), "file created", toast.length_long).show(); } catch (exception e) { e.getstacktrace(); toast.maketext(v.getcontext(), "could not create file", toast.length_long).show(); } //end of write json object } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } //write json object } }); } private void validate(edittext cnic, edittext mobile, edittext name, edittext address, edittext kin) { // todo auto-generated method stub if(name.gettext().tostring().trim().equals("")){ name.seterror("please enter name"); } } protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if(resultcode == activity.result_ok ) { if (requestcode==uploadfile){ // uri selectedimage = data.getdata(); // if ("content".equalsignorecase(selectedimage.getscheme())) { // string[] filepathcolumn = { "_data" }; //cursor cursor = getcontentresolver().query(selectedimage, // filepathcolumn, null, null, null); //cursor.movetofirst(); //int columnindex = cursor.getcolumnindex("_data");} // string picturepath = cursor.getstring(columnindex); // cursor.close(); // } // else if ("file".equalsignorecase(selectedimage.getscheme())) { // string path= selectedimage.getpath();} /* uri selectedimage = data.getdata(); string[] filepathcolumn = { "data"}; cursor cursor = getcontentresolver().query(selectedimage, filepathcolumn, null, null, null); cursor.movetofirst(); int columnindex = cursor.getcolumnindex(filepathcolumn[0]); string picturepath = cursor.getstring(columnindex); cursor.close();*/ } if (requestcode == take_image) { //get image thumbnail = (bitmap) data.getextras().get("data"); bitmapfactory.options factoryoptions = new bitmapfactory.options(); factoryoptions.injustdecodebounds = true; int imagewidth = factoryoptions.indensity=50; int imageheight = factoryoptions.indensity=50; image2 = bitmap.createscaledbitmap(thumbnail, imagewidth , imageheight, true); //////listview work listviewattachment.setitemscanfocus(true); myadapter = new myadapter(); listitem listitem = new listitem(); myitems.add(listitem); listviewattachment.setadapter(myadapter); myadapter.notifydatasetchanged(); ////////////////////end of listview work bytearrayoutputstream bytes = new bytearrayoutputstream(); thumbnail.compress(bitmap.compressformat.jpeg, 100, bytes); //encode image byte[] b = bytes.tobytearray(); encodedimagestring = base64.encodetostring(b, base64.default); } } } public class myadapter extends baseadapter { private layoutinflater minflater; public myadapter() { minflater = (layoutinflater) getsystemservice(context.layout_inflater_service); } public int getcount() { return myitems.size(); } public listitem getitem(int position) { return myitems.get(position); } public long getitemid(int position) { return position; } public view getview(final int position, view convertview, viewgroup parent) { final viewholder holder; if (convertview == null) { holder = new viewholder(); convertview = minflater.inflate(r.layout.imageview2, null); holder.image = (imageview) convertview .findviewbyid(r.id.imageview2); holder.delete=(button)convertview.findviewbyid(r.id.buttonclose); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.image.setimagebitmap(image2); holder.image.settag(position); holder.delete.settag(position); holder.image.setonclicklistener(new onclicklistener() { @override public void onclick(view view) { // todo auto-generated method stub final dialog imgdialog = new dialog(view.getcontext(),android.r.style.theme_translucent_notitlebar_fullscreen); imgdialog.requestwindowfeature(window.feature_no_title); imgdialog.setcancelable(false); // layout imageview2 used because when use simple imageview layout dialogue imageview , closebutton, // every taken image @ instancewill not shown in dialogue. imgdialog.setcontentview(r.layout.imageview); button btnclose = (button)imgdialog.findviewbyid(r.id.btnivclose); imageview ivpreview = (imageview)imgdialog.findviewbyid(r.id.image1); bitmapfactory.options factoryoptions = new bitmapfactory.options(); int imagewidth = factoryoptions.indensity=400; int imageheight = factoryoptions.indensity=500; //thumbnail selected coz if select bm enlarge give poor quality(bm small sized image) bitmap scaled = bitmap.createscaledbitmap(customerregistrationl0.thumbnail, imagewidth, imageheight, true); ivpreview.setimagebitmap(scaled); btnclose.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { imgdialog.dismiss(); } }); imgdialog.show(); // listitem listitem = new listitem(); //myitems.add(listitem); myadapter.notifydatasetchanged(); //listviewattachment.setselection(myadapter.getcount()+1 ); } }); holder.delete.setonclicklistener(new onclicklistener() { @override public void onclick(view view) { // todo auto-generated method stub int tag = (integer) view.gettag(); if (tag != (myitems.size() )) { myitems.remove(tag); log.d("gcm", "item removed " + tag); myadapter.notifydatasetchanged(); } } }); return convertview; } } class viewholder { imageview image; button delete; } }
this because in getview() not consider position. set bitmap image2 recent bitmap: holder.image.setimagebitmap(image2);
i think misunderstood how view works. getview() method called each item in list when notifydatasetchanged().
the solution keep reference different bitmaps in list (or elsewhere.. giving possible solution),
list<bitmap> images = new arraylist<>(); when image result, add images list.
image2 = bitmap.createscaledbitmap(thumbnail, imagewidth , imageheight, true); images.add(image2) finally, setimagebitmap based on position of item during getview()
holder.image.setimagebitmap(images.get(position));
Comments
Post a Comment