android - how to move StrictMode.ThreadPolicy.Builder to Async task or to an Intent service?? if Intent mean it would be much helpfull -


 strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); 

how move strictmode.threadpolicy.builder asynctask or intentservice?? if intent mean helpful.

protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.addbeneficiary);     strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build();     b1 = (imagebutton) findviewbyid(r.id.profilephotobutton);     b2 = (imagebutton) findviewbyid(r.id.idcardicon);     b3 = (imagebutton) findviewbyid(r.id.formphotoicon);     b4=(imageview) findviewbyid(r.id.closeclick);     up=(button)findviewbyid(r.id.submitbutton);     iv1=(imageview)findviewbyid(r.id.beneficiaryprofilepic);     iv2=(imageview)findviewbyid(r.id.idcardpic);     iv3=(imageview)findviewbyid(r.id.beneficiaryformpic);     beneficiaryname=(edittext)findviewbyid(r.id.tbeneficiaryname);     proofname=(edittext)findviewbyid(r.id.tvprooftype);     idproofno=(edittext)findviewbyid(r.id.idproofnumber);     strictmode.setthreadpolicy(policy);     b1.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             intent takephotointent = new intent(mediastore.action_image_capture);             mmediauri = getoutputmediafileuri(media_type_image);             if (mmediauri == null) {                 // display error                 toast.maketext(mainactivity.this, r.string.error_external_storage,                         toast.length_long).show();             } else {                 takephotointent.putextra(mediastore.extra_output, mmediauri);                 startactivityforresult(takephotointent, 0);             }         }     });     b2.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             intent takephotointent = new intent(mediastore.action_image_capture);             mmediauri = getoutputmediafileuri(media_type_image);             if (mmediauri == null) {                 // display error                 toast.maketext(mainactivity.this, r.string.error_external_storage,                         toast.length_long).show();             } else {                 takephotointent.putextra(mediastore.extra_output, mmediauri);                 startactivityforresult(takephotointent, 1);             }         }      });     b3.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             intent takephotointent = new intent(mediastore.action_image_capture);             mmediauri = getoutputmediafileuri(media_type_image);             if (mmediauri == null) {                 // display error                 toast.maketext(mainactivity.this, r.string.error_external_storage,                         toast.length_long).show();             } else {                 takephotointent.putextra(mediastore.extra_output, mmediauri);                 startactivityforresult(takephotointent, 2);             }          }     });     b4.setonclicklistener(new  view.onclicklistener(){           public void onclick(view v){             intent ii = new intent(mainactivity.this, listofprojectsactivity.class);             finish();             startactivity(ii);         }     });     up.setonclicklistener(new view.onclicklistener(){          @override         public void onclick(view v) {             dofileupload();            /* toast.maketext(mainactivity.this,"first images path="+ivs,toast.length_long).show();             toast.maketext(mainactivity.this,"2="+ivs2,toast.length_long).show();             toast.maketext(mainactivity.this,"3="+ivs3,toast.length_long).show();*/         }     }); } private uri getoutputmediafileuri(int mediatype) {     // safe, should check sdcard mounted     // using environment.getexternalstoragestate() before doing this.     if (isexternalstorageavailable()) {         // uri         // 1. external storage directory         string appname = mainactivity.this.getstring(r.string.app_name);         file mediastoragedir = new file(                 environment.getexternalstoragepublicdirectory(environment.directory_pictures),                 appname);          // 2. create our subdirectory         if (! mediastoragedir.exists()) {             if (! mediastoragedir.mkdirs()) {                 log.e(tag, "failed create directory.");                 return null;             }         }         // 3. create file name         // 4. create file          date = new date();         string timestamp = new simpledateformat("yyyymmdd_hhmmss", locale.us).format(now);          string path = mediastoragedir.getpath() + file.separator;         if (mediatype == media_type_image) {             mediafile = new file(path + "img_" + timestamp + ".jpg");         }          else {             return null;         }          log.d(tag, "file: " + uri.fromfile(mediafile));          // 5. return file's uri         return uri.fromfile(mediafile);     }     else {         return null;     } }  private boolean isexternalstorageavailable() {     string state = environment.getexternalstoragestate();      if (state.equals(environment.media_mounted)) {         return true;     }     else {         return false;     } }   private void dofileupload(){    string tv1= beneficiaryname.gettext().tostring();   string tv2=proofname.gettext().tostring();     string tv3=idproofno.gettext().tostring();     file file1 = new file(ivs);     file file2 = new file(ivs2);     file file3=new file(ivs3);     string urlstring = "http://webdev.xerago.com/habitat/upload.php";     try     {          httpclient client = new defaulthttpclient();         httppost post = new httppost(urlstring);         filebody bin1 = new filebody (file1);         filebody bin2 = new filebody (file2);         filebody bin3=new filebody (file3);         multipartentity reqentity = new multipartentity();         list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>();         namevaluepairs.add(new basicnamevaluepair("bname", tv1));         namevaluepairs.add(new basicnamevaluepair("bproof", tv2));         namevaluepairs.add(new basicnamevaluepair("bproof_no", tv3));         reqentity.addpart("bname", new stringbody(tv1));         reqentity.addpart("bproof", new stringbody(tv2));         reqentity.addpart("bproof_no", new stringbody(tv3));         reqentity.addpart("beneficiarypic", bin1);         reqentity.addpart("idproof", bin2);         reqentity.addpart("beneform",bin3);       //  reqentity.addpart("bname", new stringbody(benename));        // reqentity.addpart("bproof", new stringbody(prooftype));        // reqentity.addpart("bproof_no", new stringbody(prooofnumber));         post.setentity(reqentity);         httpresponse response = client.execute(post);         resentity = response.getentity();         final string response_str = entityutils.tostring(resentity);         if (resentity != null) {             log.i("response",response_str);             runonuithread(new runnable(){                 public void run() {                     try {                          toast.maketext(getapplicationcontext(),"upload complete. check server uploads directory.", toast.length_long).show();                     } catch (exception e) {                         e.printstacktrace();                     }                 }             });         }     }     catch (exception ex){         log.e("debug", "error: " + ex.getmessage(), ex);     } } private void preview(int req){     try     {         bitmapfactory.options options=new bitmapfactory.options();         options.insamplesize=8;         final bitmap bitmap=bitmapfactory.decodefile(mmediauri.getpath(),options);         switch(req)         {             case 0:                 iv1.setimagebitmap(bitmap);                 iv1.settag(mmediauri.getpath());                 ivs=(string) iv1.gettag();                 break;             case 1:                 iv2.setimagebitmap(bitmap);                 iv2.settag(mmediauri.getpath());                 ivs2=(string) iv2.gettag();                 break;             case 2:                 iv3.setimagebitmap(bitmap);                 iv3.settag(mmediauri.getpath());                 ivs3=(string)iv3.gettag();                 break;             default:                 break;         }      }     catch(nullpointerexception e)     {         e.printstacktrace();     } }  @override protected void onactivityresult(int requestcode, int resultcode, intent data) {     super.onactivityresult(requestcode, resultcode, data);      if (resultcode == result_ok) {         // add gallery         intent mediascanintent = new intent(intent.action_media_scanner_scan_file);         mediascanintent.setdata(mmediauri);         sendbroadcast(mediascanintent);         preview(requestcode);      }     else if (resultcode != result_canceled) {         toast.maketext(this, r.string.general_error, toast.length_long).show();     } }   } 

actually dont have use strictmode this.

  1. use broadcast receiver detecting internet connectivity change
  2. in receiver if internet available start intent service
  3. use onhandleintent() method of intent service (which allows process task in background thread.) upload images , texts.

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -