2012-02-16 4 views
0

현재 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는 서버에 표시되지 않습니다. 내 스마트 폰이 아닌 에뮬레이터에서 프로그램을 실행하고 있습니다.

답변

0
// binary, utf-8 bytes 
header('Content-Type: bitmap; charset=utf-8'); 

효과가 없으므로 브라우저/httpclient에 비트 맵을 출력하지 않습니다.

$file = fopen('test.jpg', 'wb'); 

/tmp/test.jpg과 같이 테스트 할 전체 경로를 지정하십시오.

+0

헤더를 제거하고 경로를 변경했지만 여전히 경로에 표시되지 않음 = ( – DroidMatt

0
String executeMultipartPost(Bitmap bm,String image_name) { 
    String resp = null; 
    try { 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 

    bm.compress(CompressFormat.JPEG, 75, bos); 

    byte[] data = bos.toByteArray(); 

    HttpClient httpClient = new DefaultHttpClient(); 

    HttpPost postRequest = new HttpPost("domain.com/upload_image.php"); 

    ByteArrayBody bab = new ByteArrayBody(data, image_name); 

    MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
    reqEntity.addPart("uploaded", bab); 
    reqEntity.addPart("photoCaption", new StringBody("sfsdfsdf")); 
    postRequest.setEntity(reqEntity); 
    HttpResponse response = httpClient.execute(postRequest); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); 
    String sResponse; 
    StringBuilder s = new StringBuilder(); 
    while ((sResponse = reader.readLine()) != null) { 
    s = s.append(sResponse); 
    } 
    resp=s.toString(); 
    } catch (Exception e) { 
    // handle exception here 
    Log.e(e.getClass().getName(), e.getMessage()); 
    } 
    return resp; 


    } 
<?php 

$target = "upload/"; 

$target = $target . basename($_FILES['uploaded']['name']) ; 
$ok=1; 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{ 
echo "yes"; 
} 
else { 
echo "no"; 
} 
?>