android - Imageloader not loading image on real device -


i using imageloader class load image url , displaying in list view. imageloader loading image on emulator, when run app on real device it's not loading image.(just showing default image).

please tell me have imageloader class working on real device.

imageloader class:

public class imageloader {      memorycache memorycache = new memorycache();     filecache filecache;     private map<imageview, string> imageviews = collections             .synchronizedmap(new weakhashmap<imageview, string>());     executorservice executorservice;     // handler display images in ui thread     handler handler = new handler();      public imageloader(context context) {         filecache = new filecache(context);         executorservice = executors.newfixedthreadpool(5);     }      final int stub_id = r.drawable.products;      public void displayimage(string url, imageview imageview) {         imageviews.put(imageview, url);         bitmap bitmap = memorycache.get(url);         if (bitmap != null)             imageview.setimagebitmap(bitmap);         else {             queuephoto(url, imageview);             imageview.setimageresource(stub_id);         }     }      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);          bitmap b = decodefile(f);         if (b != null)             return b;          // download images internet         try {             bitmap bitmap = null;             url imageurl = new url(url);             httpurlconnection conn = (httpurlconnection) imageurl                     .openconnection();             conn.setconnecttimeout(30000);             conn.setreadtimeout(30000);             conn.setinstancefollowredirects(true);             inputstream = conn.getinputstream();             outputstream os = new fileoutputstream(f);             utils.copystream(is, os);             os.close();             conn.disconnect();             bitmap = decodefile(f);             return bitmap;         } catch (throwable ex) {             ex.printstacktrace();             if (ex instanceof outofmemoryerror)                 memorycache.clear();             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;             fileinputstream stream1 = new fileinputstream(f);             bitmapfactory.decodestream(stream1, null, o);             stream1.close();              // find correct scale value. should power of 2.             // recommended size 512             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;             fileinputstream stream2 = new fileinputstream(f);             bitmap bitmap = bitmapfactory.decodestream(stream2, null, o2);             stream2.close();             return bitmap;         } catch (filenotfoundexception e) {         } catch (ioexception e) {             e.printstacktrace();         }         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() {             try {                 if (imageviewreused(phototoload))                     return;                 bitmap bmp = getbitmap(phototoload.url);                 memorycache.put(phototoload.url, bmp);                 if (imageviewreused(phototoload))                     return;                 bitmapdisplayer bd = new bitmapdisplayer(bmp, phototoload);                 handler.post(bd);             } catch (throwable th) {                 th.printstacktrace();             }         }     }      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();     }  } 

my gallerytab class:--

public class gallerytab extends fragment {     gridview gridview;     progressdialog mprogressdialog;     gridviewadapter adapter;     public list<gallerylist> phonearraylist = null;     view view;     private weakreference<remotedatatask> asynctaskweakref;      public static fragment newinstance(context context) {         gallerytab f = new gallerytab();         return f;     }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,             bundle savedinstancestate) {         // inflate layout fragment         view = inflater.inflate(r.layout.activity_gallery_tab, null);         gridview = (gridview) view.findviewbyid(r.id.gridview);         setretaininstance(true);         startnewasynctask();         // new remotedatatask(this).execute();         return view;     }      // remotedatatask asynctask     private class remotedatatask extends asynctask<void, void, void> {         private weakreference<gallerytab> fragmentweakref;          private remotedatatask(gallerytab gallerytab) {             this.fragmentweakref = new weakreference<gallerytab>(gallerytab);         }          @override         protected void onpreexecute() {             super.onpreexecute();             mprogressdialog = new progressdialog(getactivity());             mprogressdialog.settitle("gallery");             mprogressdialog.setmessage("loading...");             mprogressdialog.setindeterminate(false);             mprogressdialog.show();         }          @override         protected void doinbackground(void... params) {             // create array              phonearraylist = new arraylist<gallerylist>();             try {                 (int = 0; <= 6; i++) {                     gallerylist map = new gallerylist();                     map.setgallery("http://oi39.tinypic.com/21oydxs.jpg");                     // system.out.println("print!!!!--  "+ i);                     phonearraylist.add(map);                 }             } catch (parseexception e) {                 log.e("error", e.getmessage());                 e.printstacktrace();             }              return null;         }          @override         protected void onpostexecute(void result) {             super.onpostexecute(result);             // if (this.fragmentweakref.get() != null) {             adapter = new gridviewadapter(getactivity(), phonearraylist);             // system.out.println("print size --  "+ phonearraylist.size());             gridview.setadapter(adapter);              mprogressdialog.dismiss();             // }         }     }      private void startnewasynctask() {         remotedatatask asynctask = new remotedatatask(this);         this.asynctaskweakref = new weakreference<remotedatatask>(asynctask);         asynctask.execute();     } } 

my gridviewadapter class:-

public class gridviewadapter extends baseadapter {      // declare variables     context context;     layoutinflater inflater;     imageloader imageloader;     private list<gallerylist> galleryarraylist = null;     private arraylist<gallerylist> arraylist;      public gridviewadapter(context context, list<gallerylist> phonearraylist) {         this.context = context;         this.galleryarraylist = phonearraylist;         inflater = layoutinflater.from(context);         this.arraylist = new arraylist<gallerylist>();         this.arraylist.addall(phonearraylist);         imageloader = new imageloader(context);     }      public class viewholder {         imageview phone;     }      @override     public int getcount() {         return galleryarraylist.size();     }      @override     public object getitem(int position) {         return galleryarraylist.get(position);     }      @override     public long getitemid(int position) {         return position;     }      public view getview(final int position, view view, viewgroup parent) {         final viewholder holder;         if (view == null) {             holder = new viewholder();             view = inflater.inflate(r.layout.gridview_item, null);             // locate imageview in gridview_item.xml             holder.phone = (imageview) view.findviewbyid(r.id.phone);             view.settag(holder);         } else {             holder = (viewholder) view.gettag();         }         // set results imageview         imageloader.displayimage(galleryarraylist.get(position).getgallery(),                 holder.phone);         // listen gridview item click         view.setonclicklistener(new onclicklistener() {              @override             public void onclick(view arg0) {                 // send single item click data singleitemview class                 intent intent = new intent(context, singleitemview.class);                 // pass data phone                 intent.putextra("gallery",                         (galleryarraylist.get(position).getgallery()));                 // start singleitemview class                 context.startactivity(intent);             }         });         return view;     } } 

check necessary permissions in manifest .


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -