android - How to get intent data from a runnable back to a calling intent -


i need start thread downloads image web, return location of image on external drive via intent calling activity. in example starting in mainactivity, creating intent calls downloadimage class runnable , thread located. runnable in turn calls utility class of work off of ui thread.

where stuck how work result mainactivity. when run code null intent data. can tell me problem is?

--------------------------------------------------------- mainactivity.class   ...        uri urldata = uri.parse("http://www.testurl.com/image.png");      intent downloadintent = new intent(this, downloadimage.class);     downloadintent.putextra("imageuri", urldata);      startactivityforresult(downloadintent, download_image_request);      @override protected void onactivityresult(int requestcode,                                 int resultcode,                                 intent data) {     if (resultcode == result_ok) {         if (requestcode == download_image_request) {             intent galleryintent = makegalleryintent(data.tostring());              startactivity(galleryintent);         }     }     else if (resultcode != result_ok || requestcode != download_image_request) {          toast.maketext(this,                 "activity did not complete correctly.",                 toast.length_short).show();     } }   --------------------------------------------------------- downloadimage.class  ...   public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);          final uri data = getintent().getdata();          runnable downloadrunnable = new runnable() {             @override            public void run() {             intent = new intent(downloadimage.this, mainactivity.class)                            i.putextra("urldata", downloadutils.downloadimage(downloadimageactivity.this, data));         }  // downloadimage takes in uri object , context create location on disk result.  returns uri object.                   setresult(result_ok, i);         };          thread = new thread (downloadrunnable);         thread.start();         finish();  } 

other creating own runnable, suggest use android asynctask download images web.

import android.app.activity; import android.app.progressdialog; import android.os.asynctask; import android.os.bundle;  private class downloadimage extends asynctask<void, void, void> {    progressdialog pdloading = new progressdialog(downloadimage.this);   @override   protected void onpreexecute() {       super.onpreexecute();        //this method running on ui thread       pdloading.setmessage("\tdownloading...");       pdloading.show();   }   @override   protected void doinbackground(void... params) {       //can access params params[1], params[2] ...       //this method running on background thread don't update ui  frome here       //do long running http tasks here,you dont want pass argument , u can access parent class' variable url on here         return null;   }    @override   protected void onpostexecute(void result) {       super.onpostexecute(result);        //this method running on ui thread        pdloading.dismiss();   } 

}

then can execute asynctask new downloadimage().execute(); note can pass parameters here execute(); read more on android documentation


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -