我開發一個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服務器的代碼是什麼樣子?
感謝,
波阿斯
如果你正在尋找你來錯了網站,自由職業者。 – Perception 2013-03-02 13:17:43