android - how to display images from sd card using ImageLoader -


i'm getting malformedurlexception while displaying images sd card using imageloader, getting error of java.net.malformedurlexception: protocol not found: /storage/emulated/0/classnkk_images/3_20150928162260013.png . can possible pass sd card path imageloader.

log cat

09-30 06:40:24.056  14267-14343/? w/system.err﹕ java.net.malformedurlexception: protocol not found: /storage/emulated/0/classnkk_images/3_20150928162260013.png 09-30 06:40:24.056  14267-14343/? w/system.err﹕ @ java.net.url.<init>(url.java:176) 09-30 06:40:24.056  14267-14343/? w/system.err﹕ @ java.net.url.<init>(url.java:125) 09-30 06:40:24.056  14267-14343/? w/system.err﹕ @ com.example.tazeen.classnkk.imageloader.getbitmap(imageloader.java:68) 09-30 06:40:24.056  14267-14343/? w/system.err﹕ @ com.example.tazeen.classnkk.imageloader.access$000(imageloader.java:23) 09-30 06:40:24.056  14267-14343/? w/system.err﹕ @ com.example.tazeen.classnkk.imageloader$photosloader.run(imageloader.java:134) 09-30 06:40:24.056  14267-14343/? w/system.err﹕ @ java.util.concurrent.executors$runnableadapter.call(executors.java:422) 09-30 06:40:24.056  14267-14343/? w/system.err﹕ @ java.util.concurrent.futuretask.run(futuretask.java:237) 09-30 06:40:24.056  14267-14343/? w/system.err﹕ @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) 09-30 06:40:24.056  14267-14343/? w/system.err﹕ @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) 09-30 06:40:24.056  14267-14343/? w/system.err﹕ @ java.lang.thread.run(thread.java:818)  

here imageloader class

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(8000);             conn.setreadtimeout(8000);             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();     }  } 

it seems having malformed url - error says.
therefore check if have proper url.
similar question:
malformedurlexception in android when downloading zip file.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -