Not able to upload image to the server in android
I have used some code from the internet for uploading image
public class ActUpload extends Activity {
InputStream is;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
R.drawable.blue);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.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));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://127.0.0.1:80/php/base.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
}
}
and even took some php code copied some php code which i am running in local sever using wamp server. There is no response in the local server. and this is my php code.
<?php
$base=$_REQUEST['image'];
echo $base;
// base64 encoded utf-8 string
$binary=base64_decode($base);
// binary, utf-8 bytes
header('Content-Type: bitmap; charset=utf-8');
// print($binary);
//$theF开发者_StackOverflowile = base64_decode($image_data);
$file = fopen('test.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo '<img src=test.jpg>';
?>
Can any one help me in this. thanks in advance.
A nice tutorial for uploading image to server-
http://coderzheaven.com/2011/04/android-upload-an-image-to-a-server/
Edit:
You just need to change this according to your local server:
HttpPost httppost = new HttpPost("http://<local ip address>/android/upload_image.php");
// For example,in my code,i used:
// HttpPost httppost = new HttpPost("http://192.168.100.47/android/upload_image.php");
Did you manage to solve the problem? I did get it to work when i changed $base=$_REQUEST['image']; => $base=$_POST['image']; and commented out the following line header('Content-Type: bitmap; charset=utf-8'); Btw, did try to convert the code to use HttpURLConnection api?
精彩评论