현재 Android를 통해 PHP 서버에 이미지를 업로드하려고합니다. 안드로이드에 대한 코드의Android : 이미지 업로드
// 세그먼트 PHP 서버 (upload_image.php)에 코드의
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
$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는 서버에 표시되지 않습니다. 내 스마트 폰이 아닌 에뮬레이터에서 프로그램을 실행하고 있습니다.
헤더를 제거하고 경로를 변경했지만 여전히 경로에 표시되지 않음 = ( – DroidMatt