0
我目前正試圖通過Android將圖片上傳到PHP服務器。下面是代碼:Android:上傳圖片
// Android上段代碼
bm = BitmapFactory.decodeFile(imagePath); //imagePath is the path of the image in my SD card
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 90, bao);//compressing image
byte[] ba = bao.toByteArray();
String ba1 = Base64.encodeBytes(ba);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",ba1));
try{
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://domain.com/upload_image.php");
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse res = client.execute(post);
HttpEntity entity = res.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag","Error in http connection "+e.toString());
}
// PHP服務器(upload_image.php)的代碼段
<?php
$base=$_REQUEST['image'];
// base64 encoded utf-8 string
$binary=base64_decode($base);
// binary, utf-8 bytes
header('Content-Type: bitmap; charset=utf-8');
$file = fopen('test.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
?>
我無法上傳映像到服務器上,其中test.jpg從不在服務器上顯示。我從我的智能手機運行程序,而不是仿真器。
刪除標題並改變了路徑,仍然沒有出現在path =( – DroidMatt 2012-02-16 05:03:03