0

我正在开发一个 android 应用程序,我想通过 HTTP-Post 将捕获的图像从 android 设备发送到 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-server 代码应该是什么样子的?

谢谢,

波阿斯

4

1 回答 1

1

我们可以通过以下方式获取服务器中的图像:

$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);
    }
}
于 2013-03-02T15:51:34.280 回答