我正在编写一个必须通过网络服务上传图片的安卓应用程序。我的 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();
}
}