В настоящее время я пытаюсь загрузить изображение на сервер 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 никогда не отображается на сервере. Я запускаю программу со своего смартфона, а не с эмулятора.