@Override
public void onImageCapture(@NonNull File imageFile) {
private OkHttpClient okHttpClient = new OkHttpClient();
Toast.makeText(this,
"Captured image size is : " + imageFile.length(),
Toast.LENGTH_SHORT)
.show();
Log.i("照片名", "onImageCapture: " + imageFile.getName());
Log.i("照片路径", "onImageCapture: " + imageFile.getAbsolutePath());
File file = new File(imageFile.getAbsolutePath());
RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
RequestBody requestBody = new MultipartBody.Builder()
//Filedata为与服务端设置的名称要一致
.setType(MultipartBody.FORM)
.addPart(Headers.of(
"Content-Disposition",
"form-data; name=\"Filedata\"; filename=\"" + imageFile.getName() + "\""), fileBody)
.build();
Request request = new Request.Builder()
.url(Constants.UPLOAD_URL)
.post(requestBody)
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i("照片上传失败", "failure upload!" + e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.i("照片上传成功", "success upload!" + response.body().string());
}
});
// Do something with the image...
stopSelf();
}