Android post Base64 String to PHP

This is the code for image:

ByteArrayOutputStream bao = new ByteArrayOutputStream();                

bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeBytes(ba);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",ba1));

This is the HTTP code for sending the image to the server:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://servername.com/uploadimage.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));        
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();

If you find any more difficulties ask me or a check:

In PHP header file changes :

header('Content-Type: image/jpg; charset=utf-8');
$base=$_REQUEST['image'];

Leave a Comment