Android Loading Image from URL in ListView -


i reading imageurl's rss xml file , displaying in list view,i'm able display images ,but getting exception image url's

java.io.filenotfoundexception

here code:

public class imageloader {      memorycache memorycache=new memorycache();     filecache filecache;     private map<imageview, string> imageviews=collections.synchronizedmap(new weakhashmap<imageview, string>());     executorservice executorservice;      string type;     public imageloader(context context,string type){         filecache=new filecache(context);         executorservice=executors.newfixedthreadpool(5);         this.type=type;     }       public imageloader(context context){         filecache=new filecache(context);         executorservice=executors.newfixedthreadpool(5);     }         final int stub_id = r.drawable.toriimg;     public void displayimage(string url, imageview imageview)     {         //log.d("imageloader","iam in displayimage");         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);          //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.            int required_size=70;             int width_tmp=o.outwidth, height_tmp=o.outheight;             int scale=1;             if(this.type=="songplayactivity")             {                  scale=1;              }             else if(this.type=="albumplaylistactivity")             {                   scale=1;             }             else             {              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;         }          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();     }  } 

logcat:

05-14 13:00:09.640: w/system.err(11295): java.io.filenotfoundexception: http://www.toucheradio.com/hostphotos/kiran abbaraju.jpg 05-14 13:00:09.640: w/system.err(11295):    @ org.apache.harmony.luni.internal.net.www.protocol.http.httpurlconnectionimpl.getinputstream(httpurlconnectionimpl.java:521) 05-14 13:00:09.640: w/system.err(11295):    @ com.teluguone.torilt.model.imageloader.getbitmap(imageloader.java:89) 05-14 13:00:09.640: w/system.err(11295):    @ com.teluguone.torilt.model.imageloader.access$0(imageloader.java:72) 05-14 13:00:09.640: w/system.err(11295):    @ com.teluguone.torilt.model.imageloader$photosloader.run(imageloader.java:163) 05-14 13:00:09.640: w/system.err(11295):    @ java.util.concurrent.executors$runnableadapter.call(executors.java:444) 05-14 13:00:09.640: w/system.err(11295):    @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:306) 05-14 13:00:09.640: w/system.err(11295):    @ java.util.concurrent.futuretask.run(futuretask.java:138) 05-14 13:00:09.640: w/system.err(11295):    @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1088) 05-14 13:00:09.640: w/system.err(11295):    @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:581) 05-14 13:00:09.640: w/system.err(11295):    @ java.lang.thread.run(thread.java:1019) 

could 1 suggest?

as in 1 of comments, if encode "http://" "http%3a%2f%2f". urlencoder.encode method encoding query string values rather whole urls.

so, 1 solution can be:

string query = urlencoder.encode("kiran abbaraju.jpg", "utf-8"); string url = "http://www.toucheradio.com/hostphotos/" + query; 

but cleaner solution is:

string wrongurl = "http://www.toucheradio.com/hostphotos/kiran abbaraju.jpg"; url url = new url(wrongurl); uri uri = new uri(url.getprotocol(), url.getuserinfo(), url.gethost(), url.getport(), url.getpath(), url.getquery(), url.getref()); url = uri.tourl(); 

update:

in getbitmap(string url) try:

... //from web     try {         bitmap bitmap=null;         url imageurl = new url(url);         uri uri = new uri(imageurl.getprotocol(), imageurl.getuserinfo(), imageurl.gethost(), imageurl.getport(), imageurl.getpath(), imageurl.getquery(), imageurl.getref());         imageurl = uri.tourl(); ... 

Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -