2012-09-02 3 views
0

실패 나는 안드로이드 4.0.3안드로이드 - HttpClient를 업로드 이미지

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == 1 && resultCode == Activity.RESULT_OK) 
    { 
     Uri selectedImageUri = data.getData(); 
     String selectedImagePath = getPath(selectedImageUri); 

     Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath);   
     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
     Bitmap resizedDishImage = Bitmap.createScaledBitmap(bitmap, 150, 150, false); 
     resizedDishImage.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want. 
      byte [] byte_arr = stream.toByteArray(); 
      String image_str = Base64.encodeBytes(byte_arr); 
      ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 

      nameValuePairs.add(new BasicNameValuePair("image",image_str)); 

      try{ 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost(ResearchConstants.UPDATE_IMAGE); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse response = httpclient.execute(httppost); 
       String the_string_response = convertResponseToString(response); 
       Log.d("Response", the_string_response); 
      }catch(Exception e){ 
        System.out.println("Error in http connection "+e.toString()); 
      } 

    } 
} 

public String convertResponseToString(HttpResponse response) throws IllegalStateException, IOException{ 

    String res = ""; 
    StringBuffer buffer = new StringBuffer(); 
    inputStream = response.getEntity().getContent(); 
    int contentLength = (int) response.getEntity().getContentLength(); 
    if (contentLength < 0){ 

    } 
    else{ 
      byte[] data = new byte[512]; 
      int len = 0; 
      try 
      { 
       while (-1 != (len = inputStream.read(data))) 
       { 
        buffer.append(new String(data, 0, len)); //converting to string and appending to stringbuffer….. 
       } 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
      try 
      { 
       inputStream.close(); // closing the stream….. 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
      res = buffer.toString();  // converting stringbuffer to string….. 
      //System.out.println("Response => " + EntityUtils.toString(response.getEntity())); 
    } 
    return res; 
} 

를 사용하여 서버에 이미지를 업로드하는 문제를이 내 자바 코드되어 있습니다.

$base=$_REQUEST['image']; 
$binary=base64_decode($base); 
header('Content-Type: bitmap;charset-utf-8'); 
$file = fopen('uploaded_image.png', 'wb'); 
fwrite($file, $binary); 
fclose($file); 
echo "Image Upload Complete!!, Check PHP File Directory."; 

위의 코드는 내 PHP입니다.

내가 업로드 한 이미지의 결과가 있지만 파일 디렉토리를 확인할 때. 해당 이미지 디렉토리가 없습니다 ?? 누구나 해결책이나 대안을 요청할 수 있습니까?

답변

0

주 스레드에서 HTTP 작업을 수행하고 있습니다. 이는 잘못되어 예외가 발생합니다. 하지만 그 예외를 조용히 잡아라. 로그에 간단한 인쇄 error in http connection. http 작업을 다른 스레드에서 수행하십시오.

+0

여전히 같은 결과 ... – user288231

관련 문제