android - How to set ImageView using Bitmap Array -
i'm doing 1 demo of getting image urls through json
, after getting ulr
i'm downloading images , display them gridview
. storing downloaded images i'm using bitmap
array , passing imageadapter
. , want display images on next activity on imageview
i'm using following code in second activity..
// intent data intent = getintent(); // selected image id int position = i.getextras().getint("id"); imageadapter imageadapter = new imageadapter(this); imageview imageview = (imageview) findviewbyid(r.id.full_image_view); imageview.setbackgrounddrawable(imageadapter.bm[position]);
but giving me error the method setimagedrawable(drawable) in type imageview not applicable arguments (bitmap)
. please tell me how can set image bitmap
array imageview
..
imageadapter class:
public class imageadapter extends baseadapter { private context mcontext; public bitmap bm[]=new bitmap[4]; imageview imageview; // constructor public imageadapter(context c, bitmap[] bm){ mcontext = c; this.bm = bm; } public imageadapter(context c) { mcontext = c; } @override public int getcount() { return bm.length; } @override public object getitem(int position) { return bm[position]; } @override public long getitemid(int position) { return 0; } @override public view getview(int position, view convertview, viewgroup parent) { imageview = new imageview(mcontext); imageview.setimagebitmap(bm[position]); imageview.setscaletype(imageview.scaletype.center_crop); imageview.setlayoutparams(new gridview.layoutparams(70, 70)); return imageview; } }
main class:
public class androidgridlayoutactivity extends activity { public static final bitmap bm[]=new bitmap[4]; public static imageadapter img; public static string[] imageurls; public static gridview gridview; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.grid_layout); log.d("log", "1"); new getimageurls().execute(); try { thread.sleep(5000); } catch (interruptedexception e) { e.printstacktrace(); } log.d("log", "7"); gridview = (gridview) findviewbyid(r.id.grid_view); // instance of imageadapter class log.d("log", "8"); img = new imageadapter(getapplicationcontext(), bm); log.d("log", "9"); gridview.setadapter(img); log.d("log", "10"); /** * on click event single gridview item * */ gridview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view v, int position, long id) { log.d("log", "11"); // sending image id fullscreenactivity intent = new intent(getapplicationcontext(), fullimageactivity.class); log.d("log", "12"); // passing array index i.putextra("id", position); startactivity(i); } }); } public class getimageurls extends asynctask<void, void, void> { context context; private progressdialog pdialog; // url json private static final string url= "http://xxx.x.xxx/demo/test.json"; private static final string main = "maincategory"; private static final string image = "mcatimage"; // jsonarray jsonarray loginjsonarray=null; //result url protected void onpreexecute() { log.d("log", "2"); // showing progress dialog pdialog = new progressdialog(getapplicationcontext()); pdialog.setmessage("loading..."); pdialog.setcancelable(false); //pdialog.show(); log.d("log", "3"); } protected void doinbackground(void... arg) { log.d("log", "4"); // creating service handler class instance servicehandler sh = new servicehandler(); log.d("log", "5"); // making request url , getting response string jsonstr = sh.makeservicecall(url, servicehandler.post, null); log.d("response: ", ">"+jsonstr); if(jsonstr!=null) { try { jsonobject jsonobj =new jsonobject(jsonstr); loginjsonarray=jsonobj.getjsonarray(main); imageurls=new string[loginjsonarray.length()]; for(int i=0;i<loginjsonarray.length();i++){ jsonobject l=loginjsonarray.getjsonobject(i); imageurls[i]=l.getstring(image); log.d("imageurls: ", imageurls[i]); } for(int i=0;i<imageurls.length;i++) { bm[i]=downloadimage(imageurls[i]); log.d("log", bm[i].tostring()); } } catch (jsonexception e) { e.printstacktrace(); } }else{ toast.maketext(context,"check internet connection",toast.length_short).show(); } return null; } public bitmap downloadimage(string strurl) { bitmap bitmap = null; inputstream in = null; try { int response = -1; url url = new url(strurl); log.d("downloadimage: ", url.tostring()); urlconnection conn = url.openconnection(); if (!(conn instanceof httpurlconnection)) throw new ioexception("not http connection"); try{ httpurlconnection httpconn = (httpurlconnection) conn; httpconn.setallowuserinteraction(false); httpconn.setinstancefollowredirects(true); httpconn.setrequestmethod("get"); httpconn.connect(); response = httpconn.getresponsecode(); log.d("downloadimage response: ", integer.tostring(response)); if (response == httpurlconnection.http_ok) { in = httpconn.getinputstream(); log.d("downloadimage: ", in.tostring()); } } catch (exception ex) { throw new ioexception("error connecting"); } bitmap = bitmapfactory.decodestream(in); log.d("downloadimage bitmap: ", bitmap.tostring()); in.close(); }catch (ioexception e1) { e1.printstacktrace(); } log.d("log", "6"); return bitmap; } protected void onpostexecute(integer result) { // dismiss progress dialog if(pdialog.isshowing()){ pdialog.dismiss(); } } } }
fullactivity class:
public class fullimageactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.full_image); // intent data intent = getintent(); // selected image id int position = i.getextras().getint("id"); log.d("position", integer.tostring(position)); imageadapter imageadapter = new imageadapter(this); imageview imageview = (imageview) findviewbyid(r.id.full_image_view); imageview.setimagebitmap(imageadapter.bm[position]); }
}
its simple way use setimagebitmap..
imageview.setimagebitmap(imageadapter.bm[position]);
Comments
Post a Comment