2013-03-02 36 views
0

我開發一個Android應用程序中獲取圖像,我想提供一個通過HTTP-POST發送從Android設備所拍攝的圖像的PHP服務器將發送圖像到電子郵件。如何通過後期HTTP(服務器端PHP)

這是我的客戶端代碼(應用程序):

public class MainActivity extends Activity { 

protected static final int TAKE_PHOTO_CODE = 0; 
@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Button capture = (Button) findViewById(R.id.capture_button); 
    capture.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      // We use the stock camera app to take a photo 
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri()); 
      startActivityForResult(intent, TAKE_PHOTO_CODE); 
     } 
    }); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) { 
     Uri imagePath = getImageUri(); 

     // doSomething(); 
    } 
} 

/** 
* Get the uri of the captured file 
* @return A Uri which path is the path of an image file, stored on the dcim folder 
*/ 
private Uri getImageUri() { 
    // Store image in dcim 
    File file = new File(Environment.getExternalStorageDirectory() + "/DCIM", "Photo"); 
    Uri imgUri = Uri.fromFile(file); 
     PostData(); 
    return imgUri; 
} 

public void PostData(){ 

    String url = "http://doda.hostei.com"; 
    File file2 = new File(Environment.getExternalStorageDirectory()+"/DCIM", 
      "Photo"); 
    try { 
     HttpClient httpclient = new DefaultHttpClient(); 

     HttpPost httppost = new HttpPost(url); 

     InputStreamEntity reqEntity = new InputStreamEntity(
       new FileInputStream(file2), -1); 
     reqEntity.setContentType("binary/octet-stream"); 
     reqEntity.setChunked(true); // Send in multiple parts if needed 
     httppost.setEntity(reqEntity); 
     HttpResponse response = httpclient.execute(httppost); 
     //Do something with response... 

    } catch (Exception e) { 
     // show error 
    } 

} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

} 

應如何PHP服務器的代碼是什麼樣子?

感謝,

波阿斯

+1

如果你正在尋找你來錯了網站,自由職業者。 – Perception 2013-03-02 13:17:43

回答

1

We can get the images in the server as:

$imageStoreFolder = DESTINATION_OF_IMAGE_UPLOAD_FOLDER // change this where image is uploaded 
if(isset($_FILES) && count($_FILES)){ 
    foreach($_FILES as $file){ 
     $source = $file['tmp_name']; 
     $destination = $imageStoreFolder.$file['name']; 
     move_uploaded_file($source, $destination); 
    } 
} 
+0

它列出我 '錯誤:[4]語法錯誤,意外發生T_IF在線4'(4號線是這裏的第二行) – 2013-03-02 16:50:17

+0

您是否更改了$ imageStoreFolder。它應該是服務器的位置在圖像將被存儲和「不要忘記添加;最後 如:$ imageStoreFolder = $ _ SERVER [‘DOCUMENT_ROOT’]‘/聯繫人/’;。 – 2013-03-03 14:58:52

相關問題