2012-03-03 3 views
3

내 서버에 이미지를 업로드하는 중 지금 당장 잃어 버렸습니다. 사진을 찍어 Android 기기에서 내 위치 정보를 가져올 수 있습니다. 파일을 서버에 업로드 할 때 따르는 코드가 있습니다.이미지를 서버에 업로드 PHP Android

public Boolean postFunction(File image) { 
    String tag = "postFunction"; 

    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost(UPLOAD_URL); 

    try { 
     MultipartEntity entity = new MultipartEntity(); 

     entity.addPart("type", new StringBody("photo")); 
     entity.addPart("data", new FileBody(image)); 
     httppost.setEntity(entity); 
     HttpResponse response = httpclient.execute(httppost); 
     Log.i(tag, "picture was uploaded " + response.toString()); 
     return true; 

    } catch (ClientProtocolException e) { 
     Log.e(tag, "Client: " + e.toString()); 
     return false; 
    } catch (IOException e) { 
     Log.e(tag, "IO: " + e.toString()); 
     return false; 
    } 
} 

이 기능에 이미지 파일을 전달하고 업로드합니다. 그러나 오류는 내가 믿는 서버 측에있다. 여전히

 public function upload() { 
     //$type = $this->input->post('type'); 
     //$data = $this->input->post('data'); 

     $base = $_REQUEST['data']; 

     echo $base; 

// base64 encoded utf-8 string 

     $binary = base64_decode($base); 

// binary, utf-8 bytes 

     header('Content-Type: bitmap; charset=utf-8'); 

// print($binary); 
//$theFile = base64_decode($image_data); 

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

     fwrite($file, $binary); 

     fclose($file); 

     echo '<img src=test.jpg>'; 
    } 

파일을 터치 도착하지만 아무 것도 표시되지 :

여기 내 PHP 코드입니다. 내가 여기서 뭔가를 놓치고 있니? 제발 도와주세요, 나는 인터넷 검색을 시도했지만 많이 도움이되지 않은 다른 결과가 떠올랐다.

답변

3

아주 좋은 자습서 here을 확인할 수 있습니다.

또한 (

public class TryprojectActivity extends Activity { 
    InputStream is; 
    int pic_count = 0; 
    Bitmap bitmap=null; 
    FileInputStream in1,in2,in3; 
    BufferedInputStream buf; 

@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.main); 

     try { 
      in1 = new FileInputStream("/sdcard/1.jpg"); 
     } 
     catch (FileNotFoundException e2) { 
     // TODO Auto-generated catch block 
      e2.printStackTrace(); 
     } 

     try { 
      in2 = new FileInputStream("/sdcard/2.jpg"); 
     } catch (FileNotFoundException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 

    try { 
     in3 = new FileInputStream("/sdcard/3.jpg"); 
    } 
    catch (FileNotFoundException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 

    Bitmap bitmapOrg1 = BitmapFactory.decodeStream(in1); 
    ByteArrayOutputStream bao1 = new ByteArrayOutputStream(); 
    bitmapOrg1.compress(Bitmap.CompressFormat.JPEG, 90, bao1); 
    byte [] imagearray1 = bao1.toByteArray(); 
    String ba1=Base64.encode(imagearray1); 

    Bitmap bitmapOrg2 = BitmapFactory.decodeStream(in2); 
    ByteArrayOutputStream bao2 = new ByteArrayOutputStream(); 
    bitmapOrg2.compress(Bitmap.CompressFormat.JPEG, 90, bao2); 
    byte [] imagearray2 = bao2.toByteArray(); 
    String ba2=Base64.encode(imagearray2); 

    Bitmap bitmapOrg3 = BitmapFactory.decodeStream(in3); 
    ByteArrayOutputStream bao3 = new ByteArrayOutputStream(); 
    bitmapOrg3.compress(Bitmap.CompressFormat.JPEG, 90, bao3); 
    byte [] imagearray3 = bao3.toByteArray(); 
    String ba3=Base64.encode(imagearray3); 

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3); 

    nameValuePairs.add(new BasicNameValuePair("image1",ba1)); 
    nameValuePairs.add(new BasicNameValuePair("image2",ba2)); 
    nameValuePairs.add(new BasicNameValuePair("image3",ba3)); 

    try{ 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://helpdesk.cispl.com/upload_file.php"); 
     UrlEncodedFormEntity obj = new UrlEncodedFormEntity(nameValuePairs); 
     obj.setChunked(true); 
     httppost.setEntity(obj); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     //is = entity.getContent(); 
     httpclient.getConnectionManager().shutdown(); 
    } 
    catch(Exception e){ 
     //CommonFunctions.writeLOG(ctx.getClass().toString(), e.toString()); 
     //CommonFunctions.showToast(ctx, "Unable to post captured image file: " + 
     //e.toString()); 
    } 
} 
0

당신이 유닉스 기반 플랫폼에서 아파치/PHP를 실행하는 경우, 아파치 서버가있는 파일을 저장하려고하는 디렉토리에 쓰기 권한이 있는지 확인, 다음 코드 시도 현재 코드에 따라 PHP 스크립트가있는 디렉토리와 동일한 디렉토리). 빠른 테스트로 스크립트가 들어있는 디렉토리에 chmod 0777 .을 입력하고 업로드 된 이미지가 올바르게 표시되는지 확인하십시오.

관련 문제