java - How to display images on ImageView from json data in android -


i getting json data. in json have url image. want display image in imageview. how can acheive this? here code

class loadinbox extends asynctask<string, string, string> {     /**      * before starting background thread show progress dialog      * */     @override     protected void onpreexecute() {         super.onpreexecute();         pdialog = new progressdialog(home.this);         pdialog.setmessage("loading inbox ...");         pdialog.setindeterminate(false);         pdialog.setcancelable(false);         pdialog.show();     }      /**      * getting inbox json      * */  protected string doinbackground(string... arg0) {         // building parameters         list<namevaluepair> params = new arraylist<namevaluepair>();          jsonobject json = userfunctions.homedata();          log.e("data", json.tostring());          // check log cat json reponse         log.d("inbox json: ", json.tostring());          try {             data = json.getjsonarray(tag_data);             log.d("inbox array: ", data.tostring());             // looping through messages             (int = 0; < data.length(); i++) {                 jsonobject c = data.getjsonobject(i);                  // storing each json item in variable                 string profile_img = c.getstring(tag_profile_img);                  // creating new hashmap                 hashmap<string, string> map = new hashmap<string, string>();                  // adding each child node hashmap key => value                 map.put(tag_profile_img, profile_img);                  // adding hashlist arraylist                 inboxlist.add(map);             }         } catch (jsonexception e) {             e.printstacktrace();         }          return null;     } protected void onpostexecute(string file_url) {         // dismiss dialog after getting products         pdialog.dismiss();         // updating ui background thread         runonuithread(new runnable() {             public void run() {                 /**                  * updating parsed json data listview                  * */                 listadapter adapter = new simpleadapter(                         home.this, inboxlist,                         r.layout.home_list_item, new string[] { tag_profile_img },                         new int[] { r.id.profile_img2 });                 // updating listview                 setlistadapter(adapter);             }         });      } 

here layout

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" >  <imageview      android:id="@+id/profile_img2"      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:paddingtop="8dip"     android:paddingleft="8dip"     android:paddingbottom="4dip" /> </relativelayout> 

so you'll want create asynctask given url load image, , populate control. typically this:

imageview imageview = (imageview)findbyid(r.id.blah); new imageloader( person.getimageuri(), imageview, 128, 128 ).execute(); 

the imageloader asynctask this:

public class imageloader extends asynctask<uri,integer,bitmapdrawable> {     private uri imageuri;      private imageview imageview;      private int preferredwidth = 80;     private int preferredheight = 80;      public imageloader( uri uri, imageview imageview, int scalewidth, int scaleheight ) {         this.imageuri = uri;         this.imageview = imageview;         this.preferredwidth = scalewidth;         this.preferredheight = scaleheight;     }      public bitmapdrawable doinbackground(uri... params) {     if( imageuri == null ) return null;     string url = imageuri.tostring();     if( url.length() == 0 ) return null;     httpget httpget = new httpget(url);     defaulthttpclient client = new defaulthttpclient();     httpresponse response = client.execute( httpget );     inputstream = new bufferedinputstream( response.getentity().getcontent() );     try {         bitmap bitmap = bitmapfactory.decodestream(is);         if( preferredwidth > 0 && preferredheight > 0 && bitmap.getwidth() > preferredwidth && bitmap.getheight() > preferredheight ) {             return bitmap.createscaledbitmap(bitmap, preferredwidth, preferredheight, false);         } else {             return bitmap;         }     } {         is.close();     }     } }      public void onpostexecute( bitmapdrawable drawable ) {         imageview.setimagebitmap( drawable );     } } 

then can kick asynctask off when image being bound in listview creating own subclass listadapter. you'll have ditch using simpleadapter because things aren't simple anymore. has lot of advantages load images being displayed. means small number loaded out of total. user can see data before image loads quicker access data. if did in existing asynctask you'd load every image, , user have wait every single 1 finish before data shown user. there somethings can improved this. 1 asynctask uses own thread you'll running lot of threads potentially (10 or more) @ once. can kill server lots of clients. can centralize these using executorservice (ie thread pool) you'll have ditch using asynctask , implement own facility run job off ui thread , post results on ui thread. second, images load every time user scrolls. implemented own caching scheme based on uri of image load image once , return cache. it's little code post here, these exercises reader.

also notice i'm not posting ui thread in onpostexecute(). that's because asynctask me don't have again code above shows. should remove runnable , inline code in onpostexecute().


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -