Display Image in android Imageview using Asynctask -
i have image being sent me through json string. want convert string image in android app , display image in imageview.i have problem, using asynctask , code in doinbackground method:
protected boolean doinbackground(final string... args){ jsonparser jsonparser = new jsonparser(); jsonarray json = jsonparser.getjsonfromurl(url); if(json!=null){ (int = 0; < json.length(); i++){ try{ jsonobject c = json.getjsonobject(i); string displayimagefromurl = c.getstring(imageurl); string clearurl = displayimagefromurl.substring(displayimagefromurl.indexof(",")+1); byte[] decodingstring = base64.decode(clearurl, base64.default); bitmap = bitmapfactory.decodebytearray(decodingstring, 0 , decodingstring.length); string showcreateddate = c.getstring(createddate); string showarticletitle = c.getstring(articletitle); hashmap<string, string> map = new hashmap<string, string>(); map.put(createddate, showcreateddate); map.put(articletitle, showarticletitle); jsonlist.add(map); }catch (exception e){ e.printstacktrace(); } } }else{ } return null; }
and code in onpostexecute() method:
protected void onpostexecute(final boolean success){ if (dialog.isshowing()) { dialog.dismiss(); } log.d("image please",bitmap.tostring()); imageview showimage = (imageview) findviewbyid(r.id.imageshow); showimage.setimagebitmap(bitmap); listadapter adapter = new simpleadapter(context, jsonlist, r.layout.activity_news, new string[] {createddate, articletitle}, new int[] { r.id.createdate, r.id.articletitle}); setlistadapter(adapter); lv = getlistview(); }
unfortunately nothings shows in logcat. can me? miss??
here pseudocode. if use in project should working.
//my image (android logo) private string base64 = "ivborw0kggoaaaansuheugaaabgaaaaycayaaadgdz34aaacguleqvriiz2wv4vuqbthp2/zbflefrgs44inilcggggwh4yvoix4f9giftyifgqwngqchfoxhbywxuwjf05rwe0umwe5ri7lejugusppyvjjkk32xacbvjpz8n3v++zlzqhdyumrg/9+bpxbt4+/lzlevdj4xs5i54olok/ocsauwfy4al/rsjanry0gx4iwfdvf0z8xqcew5anwcbiinumqfe1ajqax53f0koinfxp4cqsnoa4sgina1sbmp7tx9nrjvshocmgpeahk6lclvteweczwghqlmt7bfwoqgr+fwydhgc/ac9buzstqwnexqapydljdttkzjyqnpwt9clkojkobcyxffjh4u5btqzhuz1uagoasdjbgt1jqolalzkhrpsbbtbcpmhw4wy4cdqxo1wr1lbnwvcknajhxlwncacbauqjp5yuwtm0n6a2qc6anqr4ccdpvfv7ynyvlgxognwspdcrsl0utp6okzag4rogpfanwiz3o5ejn9kq6cjvlgqqofvkaeq78lo8jt4/2kl7u06sfqljzo74tf2nxajzctdaz02xogzbvctyjwwitc9qwlm7ycvcx1kzws6ltteztrshrvgivcdlu4hklshvwaxq7ps1zsid0/0tecit5eooquav6dmsi6ahq3vdde9gmcewqrddxzroaebt/cxgfhakgqhfe/gf+5nga7m9xprat6btkchcmx12+mq8rdzgljbq2nbk9sxm6uvsxu5ajwag40yshjiszcnmescljjc5pkwthzhqgmt6vwlftxbevhdttvppvzgdx6zxdetmts/qjwrstopxkrezwgb/5b91zea+6bfwg7fxhafaxfjiqh9rxb18aaaaasuvork5cyii="; //somewhere in code imageview = (imageview) view.findviewbyid(r.id.imageview);//layout params wrap , wrap //weak reference safe using imageview when app i.e exit , task still running weakreferenceimageview = new weakreference<imageview>(imageview); new asynctask<void, void, bitmap>() { @override protected bitmap doinbackground(void... params) { byte[] encode = base64.decode(base64.getbytes(), 0); bitmap bitmap = bitmapfactory.decodebytearray(encode, 0, encode.length); return bitmap; } @override protected void onpostexecute(bitmap bitmap) { super.onpostexecute(bitmap); if (bitmap != null) { if (weakreferenceimageview != null) { imageview weak = weakreferenceimageview.get(); if (weak != null) { weak.setimagebitmap(bitmap); } } } } }.execute();
if decoding wrong, returned bitmap null. please try image (it small) if works. if works means wrong based64 image.
fixed example, added weakreference sample of coding.
Comments
Post a Comment