Android - Encoding image and sending it to the server -
i working on encoding selected image gallery , want send server. succeeded send name , description of product text form. but, don't know how encode image , send string form. on server side, need decode php. code is...
public class addeditwishlists extends activity { // client-server - start ////////////////////// jsonparser jsonparser = new jsonparser(); // 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"; inputstream is; // 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"); string encodedimage = base64.encodetostring(image , base64.default); 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(); //string photo = inputphoto.getcontext().tostring(); encodedimage = yourselectedimage.tostring(); // building parameters list<namevaluepair> params1 = new arraylist<namevaluepair>(); params1.add(new basicnamevaluepair("name", name)); params1.add(new basicnamevaluepair("description", description)); params1.add(new basicnamevaluepair("photo", encodedimage)); log.v("log_tag", system.currenttimemillis()+".jpg"); // 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); log.v("log_tag", "in try loop" ); if (success == 1) { // closing screen finish(); } else { // failed create product } } catch (jsonexception e) { e.printstacktrace(); } 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); } } } what working on part here
if (extras != null) { id=extras.getint("id"); inputname.settext(extras.getstring("name")); inputnote.settext(extras.getstring("note")); image = extras.getbytearray("blob"); //here..i encoded image ***********is correct?********** string encodedimage = base64.encodetostring(image , base64.default); if (image != null) { if (image.length > 3) { inputphoto.setimagebitmap(bitmapfactory.decodebytearray(image,0,image.length)); } } } and...
// client-server - start ////////////////////////////////////// string name = inputname.gettext().tostring(); string description = inputnote.gettext().tostring(); // line should modified guess.... string encodedimage = yourselectedimage.tostring(); // building parameters list<namevaluepair> params1 = new arraylist<namevaluepair>(); params1.add(new basicnamevaluepair("name", name)); params1.add(new basicnamevaluepair("description", description)); // line should modified guess.. params1.add(new basicnamevaluepair("photo", encodedimage)); log.v("log_tag", system.currenttimemillis()+".jpg"); // 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()); i wrote annotation should corrected.... think part makes error..
and 1 more question on server side, how format should receive encoded photo? varchar? char?..
thank in advance.
Comments
Post a Comment