Android - Send Image file to the Server DB -


users save 3 data: name, note , image. can see code below, succeeded code send name , note server side. but, have no idea how send selected image file device serverdb.

public class addeditwishlists extends activity {      // client-server - start //////////////////////     // progress dialog     private progressdialog pdialog;      jsonparser jsonparser = new jsonparser();     edittext inputname;     edittext inputdesc;      // url create new product     private static string url_create_product =          "http://10.56.43.91/android_connect/create_product.php";       // json node names     private static final string tag_success = "success";     // client-server - end //////////////////////       //define variables     private edittext inputname;     private edittext inputnote;     private button upload;     private bitmap yourselectedimage;     private imageview inputphoto;     private button save;     private int id;     private byte[] blob=null;     byte[] image=null;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.add_wishlist);         setupviews();     }      private void setupviews() {          inputname = (edittext) findviewbyid(r.id.inputname);         inputnote = (edittext) findviewbyid(r.id.inputnote);         inputphoto = (imageview) findviewbyid(r.id.inputphoto);           bundle extras = getintent().getextras();          if (extras != null) {             id=extras.getint("id");             inputname.settext(extras.getstring("name"));             inputnote.settext(extras.getstring("note"));              image = extras.getbytearray("blob");              if (image != null) {                 if (image.length > 3) {                     inputphoto.setimagebitmap(bitmapfactory.decodebytearray(image,0,image.length));                 }             }          }            //image upload button         upload = (button) findviewbyid(r.id.upload);         upload.setonclicklistener(new view.onclicklistener() {              @override             public void onclick(view v) {                 intent intent = new intent(intent.action_get_content);                 intent.settype("image/*");                 startactivityforresult(intent, 0);             }         });          // save data         save = (button) findviewbyid(r.id.save);          // save하면 발생되는 이벤트         save.setonclicklistener(new view.onclicklistener() {              @override             public void onclick(view v) {                  if (inputname.gettext().length() != 0) {                     asynctask<object, object, object> savecontacttask = new asynctask<object, object, object>() {                         @override                         protected object doinbackground(object... params) {                             savecontact();                              // client-server - start //////////////////////////////////////                             string name = inputname.gettext().tostring();                             string description = inputnote.gettext().tostring();                               // building parameters                             list<namevaluepair> params1 = new arraylist<namevaluepair>();                             params1.add(new basicnamevaluepair("name", name));                             params1.add(new basicnamevaluepair("description", description));                              // getting json object                             // note create product url accepts post method                             jsonobject json = jsonparser.makehttprequest(url_create_product, "post", params1);                              // check log cat fro response                             log.d("create response", json.tostring());                              // check success tag                             try {                                 int success = json.getint(tag_success);                                  if (success == 1) {                                     // created product                                     // closing screen                                     finish();                                 } else {                                     // failed create product                                 }                             } catch (jsonexception e) {                                 e.printstacktrace();                             }                              // client-server - end ////////////////////////////////////                              return null;                         }                          @override                         protected void onpostexecute(object result) {                             finish();                         }                     };                      savecontacttask.execute((object[]) null);                  } else {                     alertdialog.builder alert = new alertdialog.builder(                             addeditwishlists.this);                     alert.settitle("error in save wish list");                     alert.setmessage("you need enter name of product");                     alert.setpositivebutton("ok", null);                     alert.show();                 }             }         });     }       // if users save data, act (data -> db)      private void savecontact() {          if(yourselectedimage!=null){             bytearrayoutputstream outstr = new bytearrayoutputstream();             yourselectedimage.compress(compressformat.jpeg, 100, outstr);             blob = outstr.tobytearray();         }          else{blob=image;}          // change text type string type save in db         sqliteconnector sqlcon = new sqliteconnector(this);          if (getintent().getextras() == null) {             sqlcon.insertwishlist(inputname.gettext().tostring(), inputnote.gettext().tostring(), blob);         }          else {             sqlcon.updatewishlist(id, inputname.gettext().tostring(), inputnote.gettext().tostring(),blob);         }       }      @override     protected void onactivityresult(int requestcode, int resultcode,intent resultdata) {         super.onactivityresult(requestcode, resultcode, resultdata);         switch (requestcode) {         case 0:             if (resultcode == result_ok) {                 uri selectedimage = resultdata.getdata();                 string[] filepathcolumn = { mediastore.images.media.data };                  cursor cursor = getcontentresolver().query(selectedimage,                         filepathcolumn, null, null, null);                 cursor.movetofirst();                  int columnindex = cursor.getcolumnindex(filepathcolumn[0]);                 string filepath = cursor.getstring(columnindex);                  cursor.close();                 // convert file path bitmap image using below line.                 yourselectedimage = bitmapfactory.decodefile(filepath);                 inputphoto.setimagebitmap(yourselectedimage);             }          }     }  } 

how can code between client-server annotation send image file?..

you can convert selected image base 64 string , pass string server , in server decode that,

check code convert image base64,if have imageview can write code,

imageview.builddrawingcache(); bitmap bm = imageview.getdrawingcache(); bytearrayoutputstream baos = new bytearrayoutputstream();   bm.compress(bitmap.compressformat.jpeg, 100, baos); //bm bitmap object    byte[] b = baos.tobytearray();  string encodedimage = base64.encodetostring(b , base64.default); 

edit

as in code,you have byte array in line,

image = extras.getbytearray("blob"); 

so can directly write line of code after that,

string encodedimage = base64.encodetostring(image , base64.default); 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -