Uploading image from android to php server is not working properly -
i´m trying upload image using openurlconnection i´m facing problems. java class:
public class upload extends appcompatactivity { inputstream inputstream; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_upload); strictmode.enabledefaults(); bitmap bitmap = bitmapfactory.decoderesource(getresources(), r.drawable.sss); bytearrayoutputstream stream = new bytearrayoutputstream(); bitmap.compress(bitmap.compressformat.png, 90, stream); byte [] byte_arr = stream.tobytearray(); string encodedimage = base64.encodetostring(byte_arr, base64.default); string msj = downloadimage(encodedimage); toast.maketext(getbasecontext(), "mensaje "+msj, toast.length_short).show(); } public string downloadimage(string tabla) { string urlg = "http://192.168.43.98/jober/"; try { url url = new url(urlg+"upload.php"); httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setrequestmethod("post"); // para activar el metodo post conn.setdooutput(true); conn.setdoinput(true); dataoutputstream wr = new dataoutputstream( conn.getoutputstream()); wr.writebytes("image="+tabla); wr.flush(); wr.close(); inputstream = conn.getinputstream(); bufferedreader rd = new bufferedreader(new inputstreamreader(is)); string line; stringbuffer response = new stringbuffer(); while((line = rd.readline()) != null) { response.append(line); response.append('\r'); } rd.close(); return response.tostring(); } catch(exception e){ return "error";} } }
and php code is:
<?php $base=$_request['image']; $binary=base64_decode($base); header('content-type: bitmap; charset=utf-8'); $file = fopen('uploaded_image.png', 'wb'); fwrite($file, $binary); fclose($file); echo 'image upload complete!!, please check php file directory……'; ?>
the problem when check file , looks like:
i looking error i´m not able to.
you have urlencode string before send php, since default base64 decode includes symbols + , =. change
string encodedimage = base64.encodetostring(byte_arr, base64.default);
to
string encodedimage = urlencoder.encode(base64.encodetostring(byte_arr, base64.default), "utf-8");
and go.
Comments
Post a Comment