android - Loading image bitmap from content providers using asynctask -


i have been trying query images on sd card through mediastore content provider , display thumbnail on gridview.

however, if load image thumbnail on main thread, scrolling gets incredibly slow...

so tried load bitmap through asynctasks: scrolling performance got better, grid items keep reloading thumbnail until gets correct bitmap...

here asynctask, loads bitmaps:

package x.y;  import java.lang.ref.weakreference;  import android.content.contentresolver; import android.graphics.bitmap; import android.graphics.bitmapfactory.options; import android.os.asynctask; import android.provider.mediastore; import android.widget.imageview;  public class imagethumbnailloader extends asynctask<long, void, bitmap> {      private final options moptions;      private weakreference<imageview> mimageviewweakreference;     private contentresolver mcontentresolver;          public imagethumbnailloader(imageview imageview,                 contentresolver cr) {         mcontentresolver = cr;         mimageviewweakreference = new weakreference<imageview>(imageview);         moptions = new options();         moptions.insamplesize = 4;     }        @override     protected bitmap doinbackground(long... params) {         bitmap result;             result = mediastore.images.thumbnails.getthumbnail(                     mcontentresolver, params[0],                     mediastore.images.thumbnails.mini_kind, moptions);         return result;     }      @override     protected void onpostexecute(bitmap result) {         super.onpostexecute(result);             if (mimageviewweakreference != null                     && mimageviewweakreference.get() != null)                 mimageviewweakreference.get().setimagebitmap(result);     }  } 

and here custom cursor adapter:

package x.y;  import android.content.context; import android.database.cursor; import android.graphics.bitmapfactory.options; import android.provider.mediastore; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.cursoradapter; import android.widget.imageview;  public class mediacursoradapter extends cursoradapter {      private layoutinflater minflater;     private final static int mcolumnid = 0;     private options moptions;      public mediacursoradapter(context context, cursor c) {         super(context, c);          minflater = layoutinflater.from(context);         moptions = new options();         moptions.insamplesize = 4;     }      @override     public void bindview(view view, context context, cursor cursor) {          viewholder holder = (viewholder) view.gettag();         imagethumbnailloader imageloader = new imagethumbnailloader(holder.thumbimg,                 context.getcontentresolver());         imageloader.execute(cursor.getlong(mcolumnid)); //      holder.thumbimg.setimagebitmap(mediastore.images.thumbnails.getthumbnail( //                  context.getcontentresolver(), cursor.getlong(mcolumnid), //                  mediastore.images.thumbnails.mini_kind, moptions));         log.i("prototype", "bindview : " + cursor.getposition());     }      @override     public view newview(context context, cursor cursor, viewgroup parent) {         log.i("prototype", "newview : " + cursor.getposition());         view view = minflater.inflate(r.layout.grid_item, null);         viewholder holder = new viewholder(view);         view.settag(holder);         return view;     }       private static class viewholder {         imageview thumbimg, dragimg;          viewholder(view base) {             thumbimg = (imageview) base.findviewbyid(r.id.thumbimage);             dragimg = (imageview) base.findviewbyid(r.id.dragimage);         }     }  } 

i query cursor code , send adapter:

query(mediastore.images.media.external_content_uri, new string[] { mediastore.images.media._id,             mediastore.images.media.data }, null, null,     mediastore.images.media._id); 

looks bindview() on custom cursor adapter gets called more supposed to... knows how can make images on gridview stop reloading while mantaining scrolling performance??

thanks in advance.

problem solved, had check if image @ start of async task same image @ end of it, on onpostexecute().

new bindview:

@override     public void bindview(view view, context context, cursor cursor) {          viewholder holder = (viewholder) view.gettag();         holder.thumbimg.setid(cursor.getposition());         imagethumbnailloader imageloader = new imagethumbnailloader(holder.thumbimg,             context.getcontentresolver());         imageloader.execute(cursor.getlong(mcolumnid));     log.i("prototype", "bindview : " + cursor.getposition());     } 

new async:

public class imagethumbnailloader extends asynctask<long, void, bitmap> {      private final options moptions;      private weakreference<imageview> mimageviewweakreference;     private contentresolver mcontentresolver;     private int mposition;           public imagethumbnailloader(imageview imageview,             contentresolver cr) {         mcontentresolver = cr;         mimageviewweakreference = new weakreference<imageview>(imageview);         moptions = new options();         moptions.insamplesize = 4;         mposition = imageview.getid();     }        @override     protected bitmap doinbackground(long... params) {         bitmap result;             result = mediastore.images.thumbnails.getthumbnail(                 mcontentresolver, params[0],                 mediastore.images.thumbnails.mini_kind, moptions);         return result;     }      @override     protected void onpostexecute(bitmap result) {         super.onpostexecute(result);             if (mimageviewweakreference != null                     && mimageviewweakreference.get() != null                         && mposition == mimageviewweakreference.get().getid())                 mimageviewweakreference.get().setimagebitmap(result);     }  } 

Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -