How to upload an image from android to php using asynctask -


everyone. stuck on project working on. want able upload image android gallery, encode image base64 string , send php web service, variable, decode image other end , wish.

so far able select image, gallery , encode base64 string , storing in android preference.

the problem is, think not string being sent php service (some truncated).

why think so? log.d showed me different strings when dumped @ different locations.

the code gets image , encodes is:-

private void galleryintent() {     intent intent = new intent();     intent.settype("image/*");     intent.setaction(intent.action_get_content);     startactivityforresult(intent.createchooser(intent, "please select file"),1); }  private string onselectfromgalleryresult (intent data) {      if (data != null) {         try {             bitmap = mediastore.images.media.getbitmap(getcontext().getcontentresolver() , data.getdata()) ;         } catch (ioexception e) {             e.printstacktrace();         }          bytearrayoutputstream bytearrayoutputstream = new bytearrayoutputstream() ;          bitmap.compress(bitmap.compressformat.jpeg, 100, bytearrayoutputstream) ;          byte[] imagebytes = bytearrayoutputstream.tobytearray() ;          log.d ("selected image gallery" , base64.encodetostring(imagebytes, base64.default)) ;          return base64.encodetostring (imagebytes, base64.default) ;     } else {         return null ;     } }  @override public void onactivityresult(int requestcode, int resultcode, intent data) {     super.onactivityresult(requestcode, resultcode, data);      sharedpreferences sharedpreferences =  getcontext().getsharedpreferences("myonactivityresultpref" , context.mode_private);     sharedpreferences.editor editor = sharedpreferences.edit() ;      if (resultcode == activity.result_ok) {          if (requestcode == 1) {             /*here handle image gotten gallery*/             string encodedgalleryimage = onselectfromgalleryresult(data);             editor.putstring("userencodedgalleryimage" , encodedgalleryimage);          } else if (requestcode == 0) {             /*here handle image take using camera*/          }          editor.apply();     } } 

here call asynctask class

private void callasynctask () {   sharedpreferences sp = getcontext().getsharedpreferences("myonactivityresultpref" , context.mode_private);      string userquestionattachement = sp.getstring("userencodedgalleryimage" , "") ;  log.d("callingencodedimage" , userquestionattachement) ; } 

the problem have log log.d ("selected image gallery" , base64.encodetostring(imagebytes, base64.default)) ; different log.d("callingencodedimage" , userquestionattachement) ;

there both have same beginning, different endings. expect see same characters.

can please me sort out?

in android,

 new uploadfileasync().execute("");      private class uploadfileasync extends asynctask<string, void, string> {      @override     protected string doinbackground(string... params) {          try {   string sourcefileuri = "/mnt/sdcard/abc.png";              httpurlconnection conn = null;             dataoutputstream dos = null;             string lineend = "\r\n";             string twohyphens = "--";             string boundary = "*****";             int bytesread, bytesavailable, buffersize;             byte[] buffer;             int maxbuffersize = 1 * 1024 * 1024;             file sourcefile = new file(sourcefileuri);              if (sourcefile.isfile()) {                  try {                     string uploadserveruri = "http://website.com/abc.php?";                      // open url connection servlet                     fileinputstream fileinputstream = new fileinputstream(                             sourcefile);                     url url = new url(uploadserveruri);                      // open http connection url                     conn = (httpurlconnection) url.openconnection();                     conn.setdoinput(true); // allow inputs                     conn.setdooutput(true); // allow outputs                     conn.setusecaches(false); // don't use cached copy                     conn.setrequestmethod("post");                     conn.setrequestproperty("connection", "keep-alive");                     conn.setrequestproperty("enctype",                             "multipart/form-data");                     conn.setrequestproperty("content-type",                             "multipart/form-data;boundary=" + boundary);                     conn.setrequestproperty("bill", sourcefileuri);                      dos = new dataoutputstream(conn.getoutputstream());                      dos.writebytes(twohyphens + boundary + lineend);                     dos.writebytes("content-disposition: form-data; name=\"bill\";filename=\""                             + sourcefileuri + "\"" + lineend);                      dos.writebytes(lineend);                      // create buffer of maximum size                     bytesavailable = fileinputstream.available();                      buffersize = math.min(bytesavailable, maxbuffersize);                     buffer = new byte[buffersize];                      // read file , write form...                     bytesread = fileinputstream.read(buffer, 0, buffersize);                      while (bytesread > 0) {                          dos.write(buffer, 0, buffersize);                         bytesavailable = fileinputstream.available();                         buffersize = math                                 .min(bytesavailable, maxbuffersize);                         bytesread = fileinputstream.read(buffer, 0,                                 buffersize);                      }                      // send multipart form data necesssary after file                     // data...                     dos.writebytes(lineend);                     dos.writebytes(twohyphens + boundary + twohyphens                             + lineend);                      // responses server (code , message)                     serverresponsecode = conn.getresponsecode();                     string serverresponsemessage = conn                             .getresponsemessage();                      if (serverresponsecode == 200) {                          // messagetext.settext(msg);                         //toast.maketext(ctx, "file upload complete.",                         //      toast.length_short).show();                          // recursivedelete(mdirectory1);                      }                      // close streams //                     fileinputstream.close();                     dos.flush();                     dos.close();                  } catch (exception e) {                      // dialog.dismiss();                     e.printstacktrace();                  }                 // dialog.dismiss();              } // end else block           } catch (exception ex) {             // dialog.dismiss();              ex.printstacktrace();         }         return "executed";     }      @override     protected void onpostexecute(string result) {      }      @override     protected void onpreexecute() {     }      @override     protected void onprogressupdate(void... values) {     } } 

in php,

    <?php        if (is_uploaded_file($_files['bill']['tmp_name'])) {     $uploads_dir = './';                             $tmp_name = $_files['bill']['tmp_name'];                             $pic_name = $_files['bill']['name'];                             move_uploaded_file($tmp_name, $uploads_dir.$pic_name);                             }                else{                    echo "file not uploaded successfully.";            }     ?> 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -