// 几个成员变量
private static final int TAKE_PHOTO = 100;
private static final int TAKE_PHOTO_GALLERY = 200;
private static final int CROP_PHOTO = 300;
private static final String IMAGE_FILE_NAME = "image.jpg";
// 拍照
private void takePicture() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), IMAGE_FILE_NAME)));
startActivityForResult(intent, TAKE_PHOTO);
}
// 从相册选取
private void takeFromCallera() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, TAKE_PHOTO_GALLERY);
}
// 回调处理
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case TAKE_PHOTO:
File file = new File(Environment.getExternalStorageDirectory(), IMAGE_FILE_NAME);
cropPhoto(Uri.fromFile(file));
break;
case TAKE_PHOTO_GALLERY:
if (intent != null) {
cropPhoto(intent.getData());
}
break;
case CROP_PHOTO:
if (intent != null) {
setImage(intent);
}
break;
}
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(Main2Activity.this, "取消", Toast.LENGTH_LONG).show();
return;
}
}
// 裁剪
private void cropPhoto(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", true);
// 裁剪框的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 2);
// 裁剪出图片的大小
intent.putExtra("outputX", 100);
intent.putExtra("outputY", 100);
intent.putExtra("return-data", true);
startActivityForResult(intent, CROP_PHOTO);
}
// 设置图片
private void setImage(Intent intent) {
Bundle extra = intent.getExtras();
if (extra != null) {
Bitmap bitmap = extra.getParcelable("data");
uploadFile("163", "1", bitmap);
image.setImageBitmap(bitmap);
}
}
// okhttp上传文件
private void uploadFile(String doctorId, String fileType, Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
InputStream in = new ByteArrayInputStream(baos.toByteArray());
MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("image/png");
RequestBody body = create(MEDIA_TYPE_MARKDOWN, in);
OkHttpClient client = new OkHttpClient();
RequestBody formBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("doctorId", doctorId)
.addFormDataPart("fileType", fileType)
.addFormDataPart("file","image.jpeg",body)
.build();
final Request request = new Request.Builder()
.url("http://123.57.190.178/doctor/frontend/web/index.php?r=api/upload/file")
.post(formBody)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Gson gson = new Gson();
BaseRepBean bean = gson.fromJson(response.body().string(), BaseRepBean.class);
String result = DESUtils.decrypt(bean.getData());
Log.e("返回的结果=", result);
}
});
}
public RequestBody create(final MediaType mediaType, final InputStream inputStream) {
return new RequestBody() {
@Override
public MediaType contentType() {
return mediaType;
}
@Override
public long contentLength() {
try {
return inputStream.available();
} catch (IOException e) {
return 0;
}
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
Source source = null;
try {
source = Okio.source(inputStream);
sink.writeAll(source);
} finally {
Util.closeQuietly(source);
}
}
};
}
网友评论