android - Loading images to custom listview - how to gradually load? -
i have custom listview
- contains imageview, text , checkbox.
i loading images dropbox (taking thumnails or else getting shared url image) , loading them custom listview using imageadapter
.
it slow load images in given folder. takes several seconds load few images, minute if there dozens, , app crashes assume out of memory exception if there lots of images.
it seems taking 2 seconds url dropbox (or bitmap if decode stream). taking 60 seconds retreive 30 image urls. excessive (not sure why taking long?)
i hoping can improve code lot faster. load images incrementally, i.e. keep ui responsive , load images in batches.
i using async task, can advise.
note have tried using universal image loader , still takes long time load images.
i should note think problem in retrieving image data dropbox. code below retrieves thumbnail file, , slow. have tried changing code instead retrieve url link image, again slow execute , loop.
so there way inside of loop images, to start displaying data in listview, before loop has completed? - don't wait until ontaskcompleted
- rather start displaying images inside of doinbackground
?
here doinbackground
of async task:
@override protected boolean doinbackground(dbxfilesystem... params) { //opens thumbnails each image contained in dropbox folder try { dbxfilesystem filesystem = params[0]; (dbxfileinfo fileinfo: filesystem.listfolder(currentpath)) { dbxfile file; //this code below slow execute try{ if(fileinfo.thumbexists) // if has thumbnail retrieve { file = filesystem.openthumbnail(fileinfo.path, thumbsize.xs, thumbformat.png); bitmap image = bitmapfactory.decodestream(file.getreadstream()); pix.add(image); // image display in custom listview paths.add(fileinfo.path); // path display in custom listview file.close(); } else { //must folder if has no thumb, add local drawable folder icon bitmap image = bitmapfactory.decoderesource(getresources(), r.drawable.dbfolder); pix.add(image); paths.add(fileinfo.path); } }catch (dbxexception e1) { e1.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } system.gc(); } } catch (exception e) { e.printstacktrace(); return false; } { loadingdialog.dismiss(); } return true; }
then in ontaskcompleted set image adapter:
public void ontaskcompleted(int requestid, arraylist<bitmap> urls, arraylist<dbxpath> path) { lstview = (listview) findviewbyid(r.id.lstview); this.paths = path; imageadapter adapter = new imageadapter(this, urls, paths); lstview.setadapter(adapter); }
this getview of imageadapter.java
:
public view getview(int position, view arg1, viewgroup arg2) { layoutinflater inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); view gridview; gridview = new view(context); // inflate image_helper layout gridview = inflater.inflate(r.layout.list_row, null); // set images imageview imageview = (imageview) gridview .findviewbyid(r.id.list_image); imageview.setimagebitmap(images.get(position)); textview textview = (textview) gridview .findviewbyid(r.id.filename); textview.settext(foldername.get(position).tostring()); return gridview; }
for can use universal image loader jar universal-image-loader-1.9.2-snapshot-with-sources.jar
then adapter this
public class linkadapter extends arrayadapter<mediamodel>{ arraylist<mediamodel> medias; activity context; imageloader imageloader; displayimageoptions options; public linkadapter(activity context, int textviewresourceid, arraylist<mediamodel> objects) { super(context, textviewresourceid, objects); this.medias = objects; this.context = context; imageloader = imageloader.getinstance(); imageloader.init(imageloaderconfiguration.createdefault(context)); options = new displayimageoptions.builder() .showimageonloading(r.drawable.ic_launcher) .showimageforemptyuri(r.drawable.ic_launcher) .showimageonfail(r.drawable.ic_launcher).cacheinmemory(true) .cacheondisc(true).considerexifparams(true) .bitmapconfig(bitmap.config.rgb_565).build(); } static class viewholder { textview title; textview description; imageview iconimage; } @override public view getview(int position, view convertview, viewgroup parent) { view v = convertview; viewholder holder; if (convertview == null) { layoutinflater vi = (layoutinflater) getcontext() .getsystemservice(context.layout_inflater_service); v = vi.inflate(r.layout.link_row, null); holder = new viewholder(); holder.title = (textview) v.findviewbyid(r.id.txt_row_title); holder.description = (textview) v.findviewbyid(r.id.txt_row_description); holder.iconimage = (imageview) v.findviewbyid(r.id.img_row_icon); v.settag(holder); } else { holder = (viewholder) v.gettag(); } holder.title.settext(medias.get(position).mediatitle); holder.description.settext(medias.get(position).mediainfo); imageloader.displayimage(medias.get(position).mediathumbimgurl, holder.iconimage,options); return v; } }
Comments
Post a Comment