upload image with username from android to server using multipart post -


i want upload image , username android application server. developped code. in database find image without username ( value 0).i think should use asynctask have no idea how make it. want use method if it's possible.

this code:

public int uploadfile(string sourcefileuri) {      string filename = sourcefileuri;     string ref_name = "toto";      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()) {         dialog.dismiss();         log.e("uploadfile", "source file not exist :" + imagepath);         return 0;     }     else     {         try {              // 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("image", filename);           conn.setrequestproperty("ref_name",ref_name);              dos = new dataoutputstream(conn.getoutputstream());             dos = new dataoutputstream(conn.getoutputstream());              dos.writebytes(twohyphens + boundary + lineend);             dos.writebytes("content-disposition: form-data; name=\"image\";filename="+ filename + ";ref_name=" + ref_name+ ""+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();              log.i("uploadfile", "http response : "+ serverresponsemessage + ": " + serverresponsecode);              if(serverresponsecode == 200){                  getactivity().runonuithread(new runnable() {                     public void run() {                          toast.maketext(getactivity(), "file upload complete.", toast.length_short).show();                     }                 });             }              //close streams //             fileinputstream.close();             dos.flush();             dos.close();          } catch (malformedurlexception ex) {              dialog.dismiss();             ex.printstacktrace();              getactivity().runonuithread(new runnable() {                 public void run() {                      // toast.maketext(new_annonce_act_step3.this, "malformedurlexception", toast.length_short).show();                 }             });              log.e("upload file server", "error: " + ex.getmessage(), ex);         } catch (exception e) {              dialog.dismiss();             e.printstacktrace();              getactivity().runonuithread(new runnable() {                 public void run() {                      toast.maketext(getactivity(), "got exception : see logcat ", toast.length_short).show();                 }             });             log.e("upload file server exception", "exception : "                     + e.getmessage(), e);         }         dialog.dismiss();         return serverresponsecode;      } // end else block }  public string getpath(uri uri) {     string[] projection = { mediastore.images.media.data };     cursor cursor = getactivity().getcontentresolver().query(uri, projection, null, null, null);     // int column_index = cursor.getcolumnindexorthrow(mediastore.images.thumbnails.data);     int column_index = cursor.getcolumnindexorthrow(mediastore.images.media.data);      cursor.movetofirst();     return cursor.getstring(column_index); } 

}

server code:private function do_upload() {     $type = explode('.', $_files["image"]["name"]);     $type = strtolower($type[count($type)-1]);      $img=uniqid(rand()).'.'.$type;                          $url = "./images/".$img;       if(in_array($type, array("jpg", "jpeg", "gif", "png")))         if(is_uploaded_file($_files["image"]["tmp_name"]))             if(move_uploaded_file($_files["image"]["tmp_name"],$url))                  return $img;      return ""; } 

for code: don't know if should post image or image , username. why make 2 codes.

    public function save_image_relation_post(){     $ref_name = $_post["ref_name"];      $url = $this->do_upload();     $this->images_model->save_image($ref_name, $url); } 

function post image only:

public function save_image_test_post(){  $url= $this->do_upload();   $this->images_model->save_image_test($url);    } 


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -