通过网络服务上传Android相机拍摄

时间:2012-06-27 07:42:54

标签: android web-services

我正在写一个Android应用程序,它必须通过Web服务上传图片。 我的Web服务部分正在运行(使用控制台应用程序进行测试) 所以我想我的错误是在android部分。

使用Android代码来拍照:

public void TakePicture(){          
    i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    //outputFileUri = Uri.fromFile( new File(Environment.getExternalStorageDirectory(), "appicture.jpeg"));
    //Log.i("URI", outputFileUri.toString());
    i.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    startActivityForResult(i, cameraData);
}

活动结果:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    try{
        super.onActivityResult(requestCode, resultCode, data);
        //if I got information from Activity result
        if(resultCode == RESULT_OK){

            Bundle extras = data.getExtras();
            _bmp = (Bitmap) extras.get("data"); 

        }else{
            //log
            Log.v("Phone", "Error picture");
        }
    }
    catch (Exception e) {
        Log.i("Cam error", e.toString());
    }
}

现在发送部分:

public void SendReport(){
    File img;
    try {
        //Web service URL
        String url = "http://10.0.2.2:51136/API/picture";

        //Convert the picture
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        _bmp.compress(Bitmap.CompressFormat.JPEG, 100, bao);
        byte[] data = bao.toByteArray();

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        //Add the picture
        entity.addPart("image",new ByteArrayBody(data, "image/jpeg", "image"));
        httppost.setEntity(entity);

        //Call
        HttpResponse response = httpclient.execute(httppost);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
}

1 个答案:

答案 0 :(得分:0)

试试这样:

    entity.addPart("image",new ByteArrayBody(data, "image.jpg"));