我正在写一个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();
}
}
答案 0 :(得分:0)
试试这样:
entity.addPart("image",new ByteArrayBody(data, "image.jpg"));