java - How to correct download multiple images(thumbnails for videos) -
i write client kaltura media platform. have activity "video". in activity must display list of video thumbnails first of all, download list of videos using kaltura api, , works fine. every video entry have field thumbnailurl, point out thumbnail video. when download thumbnails videos, it's(thumbnails) downloads in wrong order, thumbnails not downloaded, thumbnails repeated other video, , not downloaded. code of callback if downloadind videos:
private void handlefetchvideolisttask(list<videoobject> videoobjects){ linearlayout videolistprogressbarcontainer = (linearlayout)mactivity.findviewbyid(r.id.videolistprogressbarcontainer); linearlayout videolistviewcontainer = (linearlayout)mactivity.findviewbyid(r.id.videolistviewcontainer); videolistprogressbarcontainer.setvisibility(view.gone); videolistviewcontainer.setvisibility(view.visible); listview listview = (listview)videolistviewcontainer.findviewbyid(r.id.videolistview); videolistadapter adapter = new videolistadapter((layoutinflater) mactivity.getsystemservice(context.layout_inflater_service)); adapter.setlist(videoobjects); listview.setadapter(adapter); downloadthumbnailstask task = new downloadthumbnailstask(videoobjects, adapter); task.execute(); }
this code of downloadthumbnailstask:
public class downloadthumbnailstask extends asynctask<list<videoobject>, videoobject, void>{ private list<videoobject> videoobjectlist = null; private list<videoobject> toprocess = null; private videolistadapter adapter = null; public downloadthumbnailstask(list<videoobject> videoobjectlist, videolistadapter adapter){ this.videoobjectlist = videoobjectlist; this.toprocess = new arraylist<videoobject>(videoobjectlist); this.adapter = adapter; } @override protected void doinbackground(list<videoobject>... lists) { for(int i=0; i<toprocess.size(); i++){ videoobject item = videoobjectlist.get(i); string urldisplay = item.getthumbnailurl(); bitmap micon11 = null; seek { inputstream in = new java.net.url(urldisplay).openstream(); micon11 = bitmapfactory.decodestream(in); } grab (exception e) { log.e("error", e.getmessage()); e.printstacktrace(); } item.setthumbnail(micon11); micon11 = null; publishprogress(item); } homecoming null; } @override protected void onprogressupdate(videoobject... values) { if(values != null && values.length > 0){ videoobject item = values[0]; videoobjectlist.remove(item); videoobjectlist.add(item); adapter.setlist(videoobjectlist); adapter.notifydatasetchanged(); } } @override protected void onpostexecute(void avoid) { this.toprocess.clear(); this.toprocess = null; } }
this code of adapter:
public class videolistadapter extends baseadapter { private list<videoobject> list; private layoutinflater layoutinflater; public videolistadapter(layoutinflater layoutinflater){ this.layoutinflater = layoutinflater; } @override public int getcount() { homecoming list.size(); } @override public object getitem(int i) { homecoming list.get(i); } @override public long getitemid(int i) { homecoming list.get(i).getid(); } @override public view getview(int i, view view, viewgroup viewgroup) { if(view == null){ view = layoutinflater.inflate(r.layout.list_item_video, viewgroup, false); } viewholder viewholder = new viewholder(view); view.settag(viewholder); videoobject item = getvideoobjectitem(i); viewholder.duration.settext(item.getformattedduration()); viewholder.title.settext(item.getname()); if(item.getthumbnail() != null){ viewholder.thumbnail.setimagebitmap(item.getthumbnail()); } homecoming view; } private videoobject getvideoobjectitem(int position){ homecoming (videoobject) getitem(position); } public list<videoobject> getlist() { homecoming list; } public void setlist(list<videoobject> list) { this.list = list; } public static class viewholder { public final imageview thumbnail; public final textview title; public final textview duration; public viewholder(view view) { this.thumbnail = (imageview)view.findviewbyid(r.id.imgvideothumbnail); this.title = (textview)view.findviewbyid(r.id.txtvideoname); this.duration = (textview)view.findviewbyid(r.id.txtvideoduration); } } }
and result of code:
i noticed 2 things:
inside doinbackground
waiting
inputstream in = new java.net.url(urldisplay).openstream(); micon11 = bitmapfactory.decodestream(in);
to finish before downloading next image.
try this:
protected void doinbackground(list<videoobject>... lists) { for(final int i=0; i<toprocess.size(); i++){ seek { new thread(){ @override public void run() { videoobject item = videoobjectlist.get(i); string urldisplay = item.getthumbnailurl(); bitmap micon11 = null; seek { inputstream in = new java.net.url(urldisplay).openstream(); micon11 = bitmapfactory.decodestream(in); } grab (exception e) { log.e("error", e.getmessage()); e.printstacktrace(); } item.setthumbnail(micon11); publishprogress(item); } }.start(); } grab (exception e) { log.e("error", e.getmessage()); } } homecoming null; } @override protected void onprogressupdate(videoobject... values) { adapter.notifydatasetchanged(); }
i think 2 lines problem:
videoobjectlist.remove(item); videoobjectlist.add(item);
you're removing item , adding 1 time again @ bottom. phone call notifydatasetchanged after setting bitmap.
inside getview
you're creating new viewholder every time.
@override public view getview(int i, view view, viewgroup viewgroup) { viewholder viewholder; if(view == null){ view = layoutinflater.inflate(r.layout.list_item_video, viewgroup, false); viewholder = new viewholder(view); view.settag(viewholder); }else{ viewholder = view.gettag(); } videoobject item = getvideoobjectitem(i); viewholder.duration.settext(item.getformattedduration()); viewholder.title.settext(item.getname()); if(item.getthumbnail() != null){ viewholder.thumbnail.setimagebitmap(item.getthumbnail()); } homecoming view; }
java android download
No comments:
Post a Comment