android - Problem loading some dynamic png image, BitmapFactory.decodeStream retuns null -


i have listview contains image , textview, used lazy image loader code found net. problem while using bitmapfactory.decodestream gives null png images. sample link of these in getting null below

image1

image2

here code using. works fine jpg image didnt work png. tried bypass scalling part still didnt work.

package com.sensacionalista.api;  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.url; import java.util.hashmap; import java.util.stack; import android.app.activity; import android.content.context; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.widget.imageview; import com.sensacionalista.r;  public class imageloader {      //the simplest in-memory cache implementation. should replaced softreference or bitmapoptions.inpurgeable(since 1.6)     private hashmap<string, bitmap> cache=new hashmap<string, bitmap>();      private file cachedir;      public imageloader(context context){         //make background thead low priority. way not affect ui performance         photoloaderthread.setpriority(thread.norm_priority-1);          //find dir save cached images         if (android.os.environment.getexternalstoragestate().equals(android.os.environment.media_mounted))             cachedir=new file(android.os.environment.getexternalstoragedirectory(),"lazylist");         else             cachedir=context.getcachedir();         if(!cachedir.exists())             cachedir.mkdirs();     }      final int stub_id=r.drawable.loading;     public void displayimage(string url, activity activity, imageview imageview)     {         if(cache.containskey(url)){             imageview.setimagebitmap(cache.get(url));             system.out.println("catch contains url:"+url);         } else {             queuephoto(url, activity, imageview);             imageview.setimageresource(stub_id);         }         }      private void queuephoto(string url, activity activity, imageview imageview)     {         //this imageview may used other images before. there may old tasks in queue. need discard them.          photosqueue.clean(imageview);         phototoload p=new phototoload(url, imageview);         synchronized(photosqueue.photostoload){             photosqueue.photostoload.push(p);             photosqueue.photostoload.notifyall();         }          //start thread if it's not started yet         if(photoloaderthread.getstate()==thread.state.new)             photoloaderthread.start();     }      private bitmap getbitmap(string url)      {         //i identify images hashcode. not perfect solution, demo.         string filename=string.valueof(url.hashcode());         file f=new file(cachedir, filename);          //from sd cache         bitmap b = decodefile(f);         if(b!=null)             return b;          //from web         try {             bitmap bitmap = null;              inputstream is=new url(url).openstream();             outputstream os = new fileoutputstream(f);             utils.copystream(is, os);             os.close();              bitmap = decodefile(f);             //bitmap = bitmapfactory.decodestream(new fileinputstream(f));             system.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");             system.out.println("url:"+url);             system.out.println("file size:"+ f.length());             system.out.println(bitmap.getheight());             system.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");             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) {e.printstacktrace();}         catch(exception 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;         }     }      photosqueue photosqueue=new photosqueue();      public void stopthread()     {         photoloaderthread.interrupt();     }      //stores list of photos download     class photosqueue     {         private stack<phototoload> photostoload=new stack<phototoload>();          //removes instances of imageview         public void clean(imageview image)         {             for(int j=0 ;j<photostoload.size();){                 if(photostoload.get(j).imageview==image)                     photostoload.remove(j);                 else                     ++j;             }         }     }      class photosloader extends thread {         public void run() {             try {                 while(true)                 {                     //thread waits until there images load in queue                     if(photosqueue.photostoload.size()==0)                         synchronized(photosqueue.photostoload){                             photosqueue.photostoload.wait();                         }                     if(photosqueue.photostoload.size()!=0)                     {                         phototoload phototoload;                         synchronized(photosqueue.photostoload){                             phototoload=photosqueue.photostoload.pop();                         }                         bitmap bmp=getbitmap(phototoload.url);                         cache.put(phototoload.url, bmp);                         object tag=phototoload.imageview.gettag();                         if(tag!=null && ((string)tag).equals(phototoload.url)){                             bitmapdisplayer bd=new bitmapdisplayer(bmp, phototoload.imageview);                             activity a=(activity)phototoload.imageview.getcontext();                             a.runonuithread(bd);                         }                     }                     if(thread.interrupted())                         break;                 }             } catch (interruptedexception e) {                 //allow thread exit             }         }     }      photosloader photoloaderthread=new photosloader();      //used display bitmap in ui thread     class bitmapdisplayer implements runnable     {         bitmap bitmap;         imageview imageview;         public bitmapdisplayer(bitmap b, imageview i){bitmap=b;imageview=i;}         public void run()         {             if(bitmap!=null)                 imageview.setimagebitmap(bitmap);             else                 imageview.setimageresource(stub_id);         }     }      public void clearcache() {         //clear memory cache         cache.clear();          //clear sd cache         file[] files=cachedir.listfiles();         for(file f:files)             f.delete();     }  } 

i tried seeing different thread has same kind of problem gets in case while getting particularly in png images only.

are sure not working png? had same issue, in situation bmp format 1 not working.

have at: here, bottom of page. may solve problem.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -