java - Image Loader "SkImageDecoder::Factory returned null" -
i pass view imageview imageloader class have created, reason getting error "skimagedecoder::factory returned null" seems happen images, url's should fine with. load 3 out of list of ten. wondering if task wasn't thread safe or else may have missed. take below
imageloader:
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); } final int stub_id = r.drawable.failed_load; public void displayimage(string url, imageview imageview) { system.out.println(url); 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); // sd cache bitmap b = decodefile(f); if (b != null) { return b; } // web try { bitmap bitmap = null; url imageurl = new url(url); httpurlconnection conn = (httpurlconnection) imageurl .openconnection(); conn.setconnecttimeout(3000); conn.setreadtimeout(3000); conn.setinstancefollowredirects(true); inputstream = conn.getinputstream(); outputstream os = new fileoutputstream(f); ioutils.copy(is, os);// util.copystream(is, os); //todo delete 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. // todo can change image content here. final int required_size = 512; 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 = (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(); } }
unfortunately solution double checking incoming url's, wrong, , seems fine now. else having similar problem, double check loading, , files coming in, wherever trying load them.
Comments
Post a Comment