Android: starting activity with extra bitmap error -
i have created app, allows me upload picture on web. resize via php, , i'm trying load these pictures app...
i have code download image:
public bitmap getbitmapfromurl(string src) { try { java.net.url url = new java.net.url(src); httpurlconnection connection = (httpurlconnection) url.openconnection(); connection.setdoinput(true); connection.connect(); inputstream input = connection.getinputstream(); bitmap mybitmap = bitmapfactory.decodestream(input); return mybitmap; } catch (exception e) { e.printstacktrace(); return null; } }
then have asynctask, download images: (i'm trying organize picture 2 in row)
public class loadphotos extends asynctask<string,object,void>{ progressbar pb; imageview img; tablerow.layoutparams paramsimage; list<string> urlsofimages; //gained database tablelayout tl; tablerow tr; layoutparams lp; int numberofphotos=0; @override protected void onpreexecute(){ urlsofimages = new arraylist<string>(); alluserphotobitmaps.clear(); // saved bitmaps pb = (progressbar) findviewbyid(r.id.profile_photos_progress_bar); pb.setvisibility(view.visible); tl = (tablelayout) findviewbyid(r.id.profile_photos_table_layout); tl.removeallviews(); //clear photos before load new 1 tr = new tablerow(profile.this); lp = new layoutparams(layoutparams.wrap_content, layoutparams.wrap_content); tr.setlayoutparams(lp); } @override protected void doinbackground(string... arg) { alluserphotoinformations = mydb.geturlsofuserimages(arg[0],arg[1],arg[2]); //this returns list<hashmap<string,string>> , i'm storing information web database : url, country, city, latitude, longitude etc... log.w("alluserphotoinformations","size: "+ alluserphotoinformations.size()); //testing if size ok (int i=0 ; < alluserphotoinformations.size() ; i++){ bitmap bm = getbitmapfromurl(alluserphotoinformations.get(i).get("file_path")); //getting bitmap function posted above alluserphotobitmaps.add(bm); //saving bitmap list<bitmap> publishprogress(bm,i); // publishing progress object bitmap , index } try{ thread.sleep(1000); }catch(interruptedexception e){ e.printstacktrace(); } return null; } @override protected void onprogressupdate(object... arg){ img = new imageview(profile.this); paramsimage = new tablerow.layoutparams(((int)(screenwidth/2))-30, ((int)(screenwidth/2))-30); paramsimage.setmargins(10,10,0,0); img.setlayoutparams(paramsimage); img.setimagebitmap((bitmap) arg[0]); img.setvisibility(view.visible); img.setonclicklistener(new imagelistener((integer)arg[1])); //here i'm passing index onclick listener. tr.addview(img); if ( numberofphotos==1 ){ // if there's 2 photos in tablerow, add tablerow tablelayout these 2 pictures try{ tl.addview(tr, new tablelayout.layoutparams(layoutparams.match_parent, layoutparams.wrap_content)); }catch(exception e){ e.printstacktrace(); } // , create new tablerow tr = new tablerow(profile.this); tr.setlayoutparams(lp); numberofphotos--; } else { numberofphotos++; } } @override protected void onpostexecute(void arg){ pb.setvisibility(view.gone); userphotos.settext("uploaded: "+alluserphotobitmaps.size()+" photos"); if ( numberofphotos==1 ){ //if there odd number of pictures, add last tablelayout try{ tl.addview(tr, new tablelayout.layoutparams(layoutparams.match_parent, layoutparams.wrap_content)); }catch(exception e){ e.printstacktrace(); } /* vytvor novej radek */ tr = new tablerow(profile.this); tr.setlayoutparams(lp); } } }
and here show of pictures , loads 2 of them , application restarts, , restarts immediately...
at last... there's onclick method:
public class imagelistener implements onclicklistener{ int index; bitmap bm; byte[] bytearray; public imagelistener(int index) { this.index = index; try { bytearrayoutputstream stream = new bytearrayoutputstream(); alluserphotobitmaps.get(index).compress(bitmap.compressformat.jpeg, 60, stream); bytearray = stream.tobytearray(); }catch(exception e){ e.printstacktrace(); } //all log bellow contains no error log.w("bitmap",""+bytearray); log.w("country",alluserphotoinformations.get(index).get("country")); log.w("city",alluserphotoinformations.get(index).get("city")); log.w("latitude",alluserphotoinformations.get(index).get("latitude")); log.w("longitude",alluserphotoinformations.get(index).get("longitude")); log.w("description",alluserphotoinformations.get(index).get("description")); log.w("category",alluserphotoinformations.get(index).get("category")); log.w("date",alluserphotoinformations.get(index).get("date")); } @override public void onclick(view v){ try { intent = new intent(getapplicationcontext(), showphotodetails.class); log.w("onclick","1"); //ok i.putextra("bitmap",bytearray); log.w("onclick","2"); //ok i.putextra("country",alluserphotoinformations.get(index).get("country")); i.putextra("city",alluserphotoinformations.get(index).get("city")); i.putextra("latitude",alluserphotoinformations.get(index).get("latitude")); i.putextra("longitude",alluserphotoinformations.get(index).get("longitude")); i.putextra("description",alluserphotoinformations.get(index).get("description")); i.putextra("category",alluserphotoinformations.get(index).get("category")); i.putextra("date",alluserphotoinformations.get(index).get("date")); log.w("onclick","3"); // ok startactivity(i); }catch(exception e){ e.printstacktrace(); toast.maketext(profile.this,"error showing image",toast.length_short).show(); } } };
my problem is, when pictures shown (in 1 of cases) , clicked on first, there's no problem @ all... when try open example last one... logs ( "onclick","1" ; "onclick", "2" ; "onclick" , "3" ) visible, apps restarts after that...
and there's showphotodetails.class wich have intentextras in it:
@override public void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); setcontentview(r.layout.show_photo_details); log.w("show_photo_details","created"); // not ok }
and yes ! have declared activities in manifest , have permissions too...
try
@override public void onclick(view v){ try { intent = new intent(getbasecontext(), showphotodetails.class); log.w("onclick","1"); //ok i.putextra("bitmap",bytearray); log.w("onclick","2"); //ok i.putextra("country",alluserphotoinformations.get(index).get("country")); i.putextra("city",alluserphotoinformations.get(index).get("city")); i.putextra("latitude",alluserphotoinformations.get(index).get("latitude")); i.putextra("longitude",alluserphotoinformations.get(index).get("longitude")); i.putextra("description",alluserphotoinformations.get(index).get("description")); i.putextra("category",alluserphotoinformations.get(index).get("category")); i.putextra("date",alluserphotoinformations.get(index).get("date")); log.w("onclick","3"); // ok startactivity(i); }catch(exception e){ e.printstacktrace(); toast.maketext(profile.this,"error showing image",toast.length_short).show(); } }
change getapplicationcontext() getbasecontext()
Comments
Post a Comment