java - Asynchronously load images into ViewPager -


i'm trying make slideshow using viewpager. want access dropbox download images , load them fragment. use async task inside fragment fetch image , onpostexecute load image imageview. problem image doesn't shown. suppose asynctask can't update fragment content or when return rootview no longer updated. can please suggest me solution? appreciate help!

screenslideshowactivity2

public class slideshowactivity2 extends fragmentactivity {  private static final int num_pages = 5; private viewpager mpager; private pageradapter mpageradapter; list<dropboxapi.entry> content;   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_slide_show_activity2);     content = albumadapterdropbox.choosenalbum.getcontent();      mpager = (viewpager) findviewbyid(r.id.pager);     mpageradapter = new screenslidepageradapter(getsupportfragmentmanager());     mpager.setadapter(mpageradapter);  }  @override public void onbackpressed() {     if (mpager.getcurrentitem() == 0) {          super.onbackpressed();     } else {          mpager.setcurrentitem(mpager.getcurrentitem() - 1);     } }   private class screenslidepageradapter extends fragmentstatepageradapter {     public screenslidepageradapter(fragmentmanager fm) {         super(fm);     }      @override     public fragment getitem(int position) {         screenslidepagefragment fragment = new screenslidepagefragment();         bundle bundle = new bundle();         bundle.putstring("url", content.get(position).path);         fragment.setarguments(bundle);         return fragment;     }      @override     public int getcount() {         return content.size();     } } 

screenslidepagefragment

public class screenslidepagefragment extends fragment { imageview imageview;  @override public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) {     bundle bundle = getarguments();     string url = bundle.getstring("url");     viewgroup rootview = (viewgroup) inflater.inflate(             r.layout.fragment_screen_slide_page, container, false);      imageview = (imageview) rootview.findviewbyid(r.id.contentimageview);     picasso.with(getactivity().getapplicationcontext()).load(r.drawable.img2).into(imageview);     file file = new file(getactivity().getcachedir().getabsolutepath() + "/" + url);     string p;     if (!file.exists()) {         downloadimage task = new downloadimage();         task.execute(url);         try {             p = task.get();         } catch (interruptedexception | executionexception e) {             e.printstacktrace();         }     } else {         picasso.with(getactivity().getapplicationcontext()).load(file).into(imageview);      }     return rootview; }  private class downloadimage extends asynctask<string, void, string> {      @override     protected string doinbackground(string... strings) {         android.os.debug.waitfordebugger();         string cachepath = null;         try {             dropboxapi.entry e = mainactivity.mapi.metadata(strings[0], 1, null, false, null);             fileoutputstream mfos;              if (e.isdir || e.contents == null) {                 return null;             }              cachepath = getactivity().getcachedir().getabsolutepath() + "/" + e.filename();              try {                 mfos = new fileoutputstream(cachepath);             } catch (filenotfoundexception e1) {                 e1.printstacktrace();                 return null;             }             mainactivity.mapi.getfile(e.path, null, mfos, null);             log.i(downloadimagetask.class.getname(), e.filename());           } catch (dropboxexception e) {             e.printstacktrace();         }         return cachepath;     }      @override     protected void onpostexecute(string string) {          picasso.with(getactivity().getapplicationcontext()).load(string).into(imageview);         super.onpostexecute(string);     } } 

}

if use picassa, dont need use asynctask again. picassa take care it. here 2 links: http://code.tutsplus.com/tutorials/android-sdk-working-with-picasso--cms-22149

http://themakeinfo.com/2015/04/android-retrofit-images-tutorial/

inside of if statement can this:

picasso.with(context).load("your url stirng").into(imageview); 

remove asynctask part. dont need anymore. happy if helps. not expert. hope expert give best solution.


Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -