java - Android - Listview with custom CursorAdapter, which running asynctask crashes -


what i'm trying using custom cursoradapter, in order choose layout show , populate view items such textviews , imageview.

now not in listview items there gonna image.

my cursoradapter code -

    private static class viewholder {     textview mesg;     textview mesg2;     textview send;     imageview myimage; }  public class chatcursoradapter extends cursoradapter implements onclicklistener {      public chatcursoradapter(context context, cursor c) {         super(context, c, 0);     }      @override     public int getcount() {         return getcursor() == null ? 0 : super.getcount();     }      @override     public int getviewtypecount() {         return 2;     }      @override     public int getitemviewtype(int _position) {          cursor cursor = (cursor) getitem(_position);         return getitemviewtype(cursor);     }        private int getitemviewtype(cursor cursor) {              string sender = cursor.getstring(2);                      string saveuser =   user;                      if (saveuser.equalsignorecase(sender)){                          return 0;                     }else{                         return 1;                     }        }        @override     public void bindview(view view, context context, cursor cursor) {      viewholder holder = (viewholder) view.gettag();          string msg = cursor.getstring(3);         string msg2 = cursor.getstring(4);         string sender = cursor.getstring(2);          holder.mesg.settext(getsmiledtext(main.this,msg));         holder.mesg2.settext(getsmiledtext(main.this,msg2));         holder.send.settext(sender);           picpath = cursor.getstring(8);           if(picpath == null || picpath.isempty()){                  holder.myimage.setvisibility(view.gone);               }else{                file file = new file(picpath);                if(file.exists()){                   new asyncimagesetter(holder.myimage, picpath).execute();                  holder.myimage.setonclicklistener(this);              }                 }       @override     public view newview(context context, cursor cursor, viewgroup parent) {         viewholder holder = new viewholder();         view itemlayout = null;         switch(getitemviewtype(cursor)){         case 0:             itemlayout = getlayoutinflater().inflate(r.layout.msg_item1,parent, false);             break;         case 1:             itemlayout =  getlayoutinflater().inflate(r.layout.msg_item13, parent,false);             break;          }          itemlayout.settag(holder);         holder.mesg = (textview) itemlayout.findviewbyid(r.id.text_start);         holder.mesg2 = (textview) itemlayout.findviewbyid(r.id.text_end);         holder.send = (textview) itemlayout.findviewbyid(r.id.text_from);         holder.myimage = (imageview) itemlayout.findviewbyid(r.id.imageview_msgpic);         return itemlayout;      }    } 

as can see when there need load image imageview, i'm using asynctask in order let flow of list view scrolling more smoother.

this how asynctask code -

     public class asyncimagesetter extends asynctask<void, void, void> {       private imageview img;      private string path;      private bitmap bm;           public asyncimagesetter(imageview img, string path) {          this.img = img;          this.path = path;        }       @override      protected void doinbackground(void... params) {          bm = bitmapfactory.decodefile(path);         bm = setimagetoimageview(path);           return null;      }       @override      protected void onpostexecute(void result) {           img.settag(path);          img.setimagebitmap(bm);          //img.setvisibility(view.visible);           super.onpostexecute(result);      } 

}

well thing sure made scrolling alot more smoother, seems make app crash lot of times.

the logcat says next -

    03-24 17:07:34.125: e/androidruntime(15422): fatal exception: asynctask #2 03-24 17:07:34.125: e/androidruntime(15422): java.lang.runtimeexception: error occured while executing doinbackground() 03-24 17:07:34.125: e/androidruntime(15422):    @ android.os.asynctask$3.done(asynctask.java:299) 03-24 17:07:34.125: e/androidruntime(15422):    @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:352) 03-24 17:07:34.125: e/androidruntime(15422):    @ java.util.concurrent.futuretask.setexception(futuretask.java:219) 03-24 17:07:34.125: e/androidruntime(15422):    @ java.util.concurrent.futuretask.run(futuretask.java:239) 03-24 17:07:34.125: e/androidruntime(15422):    @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) 03-24 17:07:34.125: e/androidruntime(15422):    @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1080) 03-24 17:07:34.125: e/androidruntime(15422):    @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:573) 03-24 17:07:34.125: e/androidruntime(15422):    @ java.lang.thread.run(thread.java:841) 03-24 17:07:34.125: e/androidruntime(15422): caused by: java.lang.outofmemoryerror 03-24 17:07:34.125: e/androidruntime(15422):    @ android.graphics.bitmapfactory.nativedecodestream(native method) 03-24 17:07:34.125: e/androidruntime(15422):    @ android.graphics.bitmapfactory.decodestream(bitmapfactory.java:623) 03-24 17:07:34.125: e/androidruntime(15422):    @ android.graphics.bitmapfactory.decodefile(bitmapfactory.java:378) 03-24 17:07:34.125: e/androidruntime(15422):    @ android.graphics.bitmapfactory.decodefile(bitmapfactory.java:417) 03-24 17:07:34.125: e/androidruntime(15422):    @ at.vcity.androidim.mainchat$asyncimagesetter.doinbackground(mainchat.java:3356) 03-24 17:07:34.125: e/androidruntime(15422):    @ at.vcity.androidim.mainchat$asyncimagesetter.doinbackground(mainchat.java:1) 03-24 17:07:34.125: e/androidruntime(15422):    @ android.os.asynctask$2.call(asynctask.java:287) 03-24 17:07:34.125: e/androidruntime(15422):    @ java.util.concurrent.futuretask.run(futuretask.java:234) 03-24 17:07:34.125: e/androidruntime(15422):    ... 4 more 

so doing wrong here?

thanks kind of help

it looks me bitmap trying store in memory large stored in tablet/emulator's memory. here;

bm = bitmapfactory.decodefile(path); 

see if code works smaller file 1 coming current path (also see if works smaller list). instance of 'the straw broke camel's back'. if current application memory intensive might have go through current code , optimise memory management.

because creating asynctask each item in listview, trying hold many images in memory @ once. might need find way it. might need try loading image's thumbnail imageviews instead.

i hope helps.


an example implementation


just run through potential way implement above linked thumbnail example, might thumbnail store context passed chatcursoradapter adding following class variable (and instantiate in constructor);

context ourcontext;  public chatcursoradapter(context context, cursor c) {            super(context, c, 0);     ourcontext = context; }  @override public void bindview(view view, context context, cursor cursor) {     ...     new asyncimagesetter(holder.myimage, picpath, ourcontext.getcontentresolver()).execute();     ... } 

then (as above) might use context contentresolver instance , pass instance asynctask via it's constructor. add method in example code thumbnail custom asynctask, might this;

contentresolver cr;  public asyncimagesetter(imageview img, string path, contentresolver cr) {      this.img = img;      this.path = path;      this.cr = cr;      } ...  @override  protected void doinbackground(void... params) {     try{          bm = getthumbnail(cr, path);     }catch(exception e){}       return null;  }  private bitmap getthumbnail(contentresolver cr, string path) throws exception {      cursor ca = cr.query(mediastore.images.media.external_content_uri, new string[] { mediastore.mediacolumns._id }, mediastore.mediacolumns.data + "=?", new string[] {path}, null);     if (ca != null && ca.movetofirst()) {         int id = ca.getint(ca.getcolumnindex(mediastore.mediacolumns._id));         ca.close();         return mediastore.images.thumbnails.getthumbnail(cr, id, mediastore.images.thumbnails.micro_kind, null );     }      ca.close();     return null;  } 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -