Image file not received from android emulator to server -
i trying send image android emulator php server. java file shown below :
public class mainactivity extends activity implements onclicklistener { private static final int result_load_image = 1; private static final string serveraddress = "http://localhost"; // 14.99.139.53 imageview imagetoupload, downloadedimage; button buploadimage, bdownloadimage; edittext uploadimagename, downloadimagename; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); imagetoupload = (imageview) findviewbyid(r.id.imagetoupload); downloadedimage = (imageview) findviewbyid(r.id.downloadedimage); buploadimage = (button) findviewbyid(r.id.buploadimage); bdownloadimage = (button) findviewbyid(r.id.bdownloadimage); uploadimagename = (edittext) findviewbyid(r.id.etuploadname); downloadimagename = (edittext) findviewbyid(r.id.etdownlaodname); imagetoupload.setonclicklistener(this); buploadimage.setonclicklistener(this); bdownloadimage.setonclicklistener(this); } @override public void onclick(view v) { switch(v.getid()){ case r.id.imagetoupload: intent galleryintent = new intent(intent.action_pick, mediastore.images.media.external_content_uri); startactivityforresult(galleryintent,result_load_image ); break; case r.id.buploadimage: bitmap image = ((bitmapdrawable) imagetoupload.getdrawable()).getbitmap() ; new uploadimage(image, uploadimagename.gettext().tostring()).execute(); break; case r.id.bdownloadimage: break; } } @override public void startactivityforresult(intent intent, int requestcode) { // todo auto-generated method stub super.startactivityforresult(intent, requestcode); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { // todo auto-generated method stub super.onactivityresult(requestcode, resultcode, data); if(requestcode == result_load_image && resultcode == result_ok && data != null){ uri selectedimage = data.getdata(); imagetoupload.setimageuri(selectedimage); } } private class uploadimage extends asynctask<void, void, void>{ bitmap image; string name; public uploadimage(bitmap image, string name){ this.image = image; this.name = name; } @override protected void doinbackground(void... arg0) { bytearrayoutputstream baos = new bytearrayoutputstream(); image.compress(bitmap.compressformat.png, 100, baos); string encodedimage = base64.encodetostring(baos.tobytearray(), base64.default); arraylist<namevaluepair> datatosend = new arraylist(); datatosend.add(new basicnamevaluepair("image", encodedimage)); datatosend.add(new basicnamevaluepair("name", name)); httpparams httprequestparams = gethttprequestparams(); httpclient client = new defaulthttpclient(httprequestparams); httppost post = new httppost(serveraddress + "/savepicture.php"); try{ post.setentity(new urlencodedformentity(datatosend)); client.execute(post); }catch(exception e) { e.printstacktrace(); } return null; } @override protected void onpostexecute(void result) { // todo auto-generated method stub super.onpostexecute(result); toast.maketext(getapplicationcontext(), "image uploaded" , toast.length_short).show(); } } private httpparams gethttprequestparams(){ httpparams httprequestparams = new basichttpparams(); httpconnectionparams.setconnectiontimeout(httprequestparams, 1000 * 30); httpconnectionparams.setsotimeout(httprequestparams, 1000 * 30); return httprequestparams; } }
when upload button clicked then, toast image uploaded image file not received @ php.
the php code shown below :
<?php $name = $_post["name"]; $image = $_post["image"]; $decodedimage = base64_decode("$image"); file_put_contents("rohit/" . $name . ".jpg",$decodedimage); ?>
what might have gone wrong ?
chang
string serveraddress = "http://localhost";
to
string serveraddress = "http://10.0.2.2";
for emulator,
change machine ip when using on phone on lan
Comments
Post a Comment