How to display the image from database in android imageview -


hi in application getting json response in image there.that image want display in android imageview.in image want set imageview.

picture":"547150_156322317826149_1332901104_n.jpg

can 1 me

well, suppose got picture url, suggest try code: create simple imageview in main.xml file:

main.xml:

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="vertical" >      <textview         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="loading image url"         android:layout_margin="10dip" />      <imageview android:id="@+id/image"         android:layout_height="wrap_content"         android:layout_width="fill_parent"         android:layout_margin="10dip"/>  </linearlayout> 

open main activity , type following code. whenever wan’t show image url call following code:

androidloadimagefromurlactivity.java:

package com.androidhive.imagefromurl;  import android.app.activity; import android.os.bundle; import android.widget.imageview;  public class androidloadimagefromurlactivity extends activity {     @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.main);          // loader image - shown before loading image         int loader = r.drawable.loader;          // imageview show         imageview image = (imageview) findviewbyid(r.id.image);          // image url         string image_url = "http://url of image here";          // imageloader class instance         imageloader imgloader = new imageloader(getapplicationcontext());          // whenever want load image url         // call displayimage function         // url - image url load         // loader - loader image, displayed before getting image         // image - imageview          imgloader.displayimage(image_url, loader, image);     } } 

add following classes in project , run project:

imageloader.java

package com.androidhive.imagefromurl;  import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.inputstream; import java.io.outputstream; import java.net.httpurlconnection; import java.net.url; import java.util.collections; import java.util.map; import java.util.weakhashmap; import java.util.concurrent.executorservice; import java.util.concurrent.executors;  import android.app.activity; import android.content.context; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.widget.imageview;  public class imageloader {      memorycache memorycache=new memorycache();     filecache filecache;     private map<imageview, string> imageviews=collections.synchronizedmap(new weakhashmap<imageview, string>());     executorservice executorservice;       public imageloader(context context){         filecache=new filecache(context);         executorservice=executors.newfixedthreadpool(5);     }      int stub_id = r.drawable.ic_launcher;     public void displayimage(string url, int loader, imageview imageview)     {         stub_id = loader;         imageviews.put(imageview, url);         bitmap bitmap=memorycache.get(url);         if(bitmap!=null)             imageview.setimagebitmap(bitmap);         else         {             queuephoto(url, imageview);             imageview.setimageresource(loader);         }     }      private void queuephoto(string url, imageview imageview)     {         phototoload p=new phototoload(url, imageview);         executorservice.submit(new photosloader(p));     }      private bitmap getbitmap(string url)     {         file f=filecache.getfile(url);          //from sd cache         bitmap b = decodefile(f);         if(b!=null)             return b;          //from web         try {             bitmap bitmap=null;             url imageurl = new url(url);             httpurlconnection conn = (httpurlconnection)imageurl.openconnection();             conn.setconnecttimeout(30000);             conn.setreadtimeout(30000);             conn.setinstancefollowredirects(true);             inputstream is=conn.getinputstream();             outputstream os = new fileoutputstream(f);             utils.copystream(is, os);             os.close();             bitmap = decodefile(f);             return bitmap;         } catch (exception ex){            ex.printstacktrace();            return null;         }     }      //decodes image , scales reduce memory consumption     private bitmap decodefile(file f){         try {             //decode image size             bitmapfactory.options o = new bitmapfactory.options();             o.injustdecodebounds = true;             bitmapfactory.decodestream(new fileinputstream(f),null,o);              //find correct scale value. should power of 2.             final int required_size=70;             int width_tmp=o.outwidth, height_tmp=o.outheight;             int scale=1;             while(true){                 if(width_tmp/2<required_size || height_tmp/2<required_size)                     break;                 width_tmp/=2;                 height_tmp/=2;                 scale*=2;             }              //decode insamplesize             bitmapfactory.options o2 = new bitmapfactory.options();             o2.insamplesize=scale;             return bitmapfactory.decodestream(new fileinputstream(f), null, o2);         } catch (filenotfoundexception e) {}         return null;     }      //task queue     private class phototoload     {         public string url;         public imageview imageview;         public phototoload(string u, imageview i){             url=u;             imageview=i;         }     }      class photosloader implements runnable {         phototoload phototoload;         photosloader(phototoload phototoload){             this.phototoload=phototoload;         }          @override         public void run() {             if(imageviewreused(phototoload))                 return;             bitmap bmp=getbitmap(phototoload.url);             memorycache.put(phototoload.url, bmp);             if(imageviewreused(phototoload))                 return;             bitmapdisplayer bd=new bitmapdisplayer(bmp, phototoload);             activity a=(activity)phototoload.imageview.getcontext();             a.runonuithread(bd);         }     }      boolean imageviewreused(phototoload phototoload){         string tag=imageviews.get(phototoload.imageview);         if(tag==null || !tag.equals(phototoload.url))             return true;         return false;     }      //used display bitmap in ui thread     class bitmapdisplayer implements runnable     {         bitmap bitmap;         phototoload phototoload;         public bitmapdisplayer(bitmap b, phototoload p){bitmap=b;phototoload=p;}         public void run()         {             if(imageviewreused(phototoload))                 return;             if(bitmap!=null)                 phototoload.imageview.setimagebitmap(bitmap);             else                 phototoload.imageview.setimageresource(stub_id);         }     }      public void clearcache() {         memorycache.clear();         filecache.clear();     }  } 

filecache.java

package com.androidhive.imagefromurl;  import java.io.file; import android.content.context;  public class filecache {      private file cachedir;      public filecache(context context){         //find dir save cached images         if (android.os.environment.getexternalstoragestate().equals(android.os.environment.media_mounted))             cachedir=new file(android.os.environment.getexternalstoragedirectory(),"tempimages");         else             cachedir=context.getcachedir();         if(!cachedir.exists())             cachedir.mkdirs();     }      public file getfile(string url){         string filename=string.valueof(url.hashcode());         file f = new file(cachedir, filename);         return f;      }      public void clear(){         file[] files=cachedir.listfiles();         if(files==null)             return;         for(file f:files)             f.delete();     }  } 

memorycache.java

package com.androidhive.imagefromurl;  import java.lang.ref.softreference; import java.util.collections; import java.util.hashmap; import java.util.map; import android.graphics.bitmap;  public class memorycache {     private map<string, softreference<bitmap>> cache=collections.synchronizedmap(new hashmap<string, softreference<bitmap>>());      public bitmap get(string id){         if(!cache.containskey(id))             return null;         softreference<bitmap> ref=cache.get(id);         return ref.get();     }      public void put(string id, bitmap bitmap){         cache.put(id, new softreference<bitmap>(bitmap));     }      public void clear() {         cache.clear();     } } 

utils.java

package com.androidhive.imagefromurl;  import java.io.inputstream; import java.io.outputstream;  public class utils {     public static void copystream(inputstream is, outputstream os)     {         final int buffer_size=1024;         try         {             byte[] bytes=new byte[buffer_size];             for(;;)             {               int count=is.read(bytes, 0, buffer_size);               if(count==-1)                   break;               os.write(bytes, 0, count);             }         }         catch(exception ex){}     } } 

i hope you, best regards


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -