android - How to do a synchronous download for Amazon S3? -


so in android project have asynctask used load image imageview control. part of logic of background asynctask download image on demand if not exist on devices storage. company moved online images amazon s3 cloud storage. have followed steps create policy, include service in project, etc... , app working fine. feel had nasty make download transferutility work within asynctask. in every example of latest library api says should use transferutility download image. , every example can find uses asynchronous call download() method returns transferobserver. because running background thread of asynctask.doinbackground(), need make download transferutility synchronous. know solution came cannot considered "best practice" way of doing things looking advice how solve issue right way. ended making empty while{} loop checking see when transfer finished. there 2 possible solutions, 1 synchronous method replace transferutility.download() or 2 solution of how design nested asynchronous task.

background asynctask

@override protected bitmap doinbackground(void... params) {     bitmap bitmap = null;     boolean retryonce = true;      while (true) {         if ((!photomanager.photoexists(siteid, photoid, imagewidth, forcefullsizeimage)) && (photoid > 0)) {             // downloadphoto() needs synchronous             photomanager.downloadphoto(siteid, photoid, imagewidth, forcefullsizeimage);         }      .... code removed brevity (load image disk bitmap (memory)      return bitmap; } 

photomanager.java

public static boolean downloadphoto(int siteid, int photoid, int expectedwidth, boolean forcefullsizeimage) {     storageutils.makefolders(siteid);     string serverfilename = getphotoserverfilename(photoid, expectedwidth, forcefullsizeimage);     file photofile = getphotofile(siteid, photoid, expectedwidth, forcefullsizeimage);     awsapi awsapi = new awsapi();     return awsapi.downloadfilesync(serverfilename, photofile, jobsightapplication.getappcontext(), jobsightapplication.gettransferutility()); } 

awsapi.java

public class awsapi { private boolean transfercomplete ; private boolean transfererror; public string errormsg;  public static transferutility gettransferutility(context context) {     // initialize amazon cognito credentials provider     cognitocachingcredentialsprovider credentialsprovider = new cognitocachingcredentialsprovider(             context,             context.getresources().getstring(r.string.aws_identity_pool_id), // identity pool id             regions.us_east_1 // region     );      amazons3 s3 = new amazons3client(credentialsprovider);     return new transferutility(s3, context); }    public boolean downloadfilesync(string serverfilename, file file, context context, transferutility transferutility) {     transfercomplete = false;     transfererror = false;     errormsg = "";     string bucket = context.getresources().getstring(r.string.aws_bucket);     try {         transferobserver transferobserver = transferutility.download(                 bucket,     /* bucket download */                 serverfilename,    /* key object download */                 file        /* file download object */         );           transferobserver.settransferlistener(new transferlistener() {              @override             public void onstatechanged(int id, transferstate state) {                 if (state == transferstate.completed) {                     transfercomplete = true;                 }             }              @override             public void onprogresschanged(int id, long bytescurrent, long bytestotal) {             }              @override             public void onerror(int id, exception ex) {                 errormsg = ex.getmessage();                 transfererror = true;             }          });          // *************************************************************************         // seems working fine         // loop can not consider "right" way solve issue         while (!transfercomplete && !transfererror) {          }          if ((transfererror) && (file.exists()))             file.delete();     }     catch (exception ex) {         log.e("xxxxxxxxxx", ex.getmessage());     }     return transfercomplete; } 


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -