android - Load bitmap in recyclerView from internalStorage with Asynctask -
i'm working in android app need downloading png web , showing in recyclerview. downloading part works fine same save in internal storage. problem display, when load in asynctask in onbindviewholder, isn't fluid..... , on avd have exception resources not closed. paste adapter :
public class myadapter extends recyclerview.adapter<myadapter.myviewholder>{ private static final string tag = "myadapter"; private list<item> listitem = null; public myadapter(list<item> listitem) { this.listitem = listitem; } @override public myviewholder oncreateviewholder(viewgroup parent, int viewtype) { // log.d(logtag, "view group " + parent); view v = layoutinflater.from(parent.getcontext()).inflate(r.layout.item_row, parent, false); return new myviewholder(v); } @override public void onbindviewholder(final myviewholder holder, int position) { //log.d(logtag, " bind " + position + " holder " + holder); final item item = listitem.get(position); holder.position = position; //new loadbitmap(position, holder).executeonexecutor(asynctask.thread_pool_executor, item.getid()); } private class loadbitmap extends asynctask<string, void, bitmap> { private myviewholder holder = null; private int position = 0; public loadbitmap(int position, myviewholder holder) { this.holder = holder; this.position = position; } @override protected bitmap doinbackground(string... params) { file file = new file("/data/data/myapp/files", params[0]); fileinputstream fis = null; bitmap bitmap = null; try { fis = new fileinputstream(file); bitmap = bitmapfactory.decodestream(fis); } catch (filenotfoundexception e) { e.printstacktrace(); } { if (fis != null) { try { fis.close(); } catch (ioexception e) { e.printstacktrace(); } } } return bitmap; } @override protected void onpostexecute(bitmap bitmap) { if (bitmap != null && holder.position == position) { holder.imageview.setimagebitmap(bitmap); } } } @override public int getitemcount() { return listitem.size(); } public class myviewholder extends recyclerview.viewholder { private imageview imageview = null; int position = 0; public myviewholder(view itemview) { super(itemview); imageview = (imageview) itemview.findviewbyid(r.id.imageview_item); } }
}
i try ui thread :
holder.imageview.post(new runnable() { @override public void run() { file file = new file("/data/data/myapp/files", item.getid()); fileinputstream fis = null; bitmap bitmap = null; try { fis = new fileinputstream(file); bitmap = bitmapfactory.decodestream(fis); holder.imageview.setimagebitmap(bitmap); } catch (filenotfoundexception e) { e.printstacktrace(); } { if (fis != null) { try { fis.close(); } catch (ioexception e) { e.printstacktrace(); } } } } });
i hope it's clear enough , sorry english. thanx.
edit : fix work perfect
@override public void onbindviewholder(final myviewholder holder, int position) { item item = listitem.get(position); holder.position = position; file file = new file("/data/data/myapp/files", item.getid()); picasso.with(holder.itemview.getcontext()).load(file).resize(50,25).centercrop().into(holder.imageview); }
why not use picasso or library that?
it's easy as:
uri uri = uri.fromfile(yourfilepath); picasso.with(activity).load(uri) .resize(100, 100).centercrop().into(viewholder.imageview);
or load image in oncreateviewholder method
@override public myviewholder oncreateviewholder(viewgroup parent, int viewtype) { // log.d(logtag, "view group " + parent); view v = layoutinflater.from(parent.getcontext()).inflate(r.layout.item_row, parent, false); imageview imgview = v.findviewbyid(r.id.yourid); final int position = holder.getadapterposition(); //get item position new loadbitmap( ...) return new myviewholder(v); }
and not forget recycle bitmap bitmap.recycle()
but never start async tasks in onbindviewholder.
edit: can item with: final int position = holder.getadapterposition();
Comments
Post a Comment