从用户首选应用(Cameta,Gallery等)中选择后,我必须上传图像文件。我可以在Imageview中将结果意图显示为位图。现在,我希望在单击按钮后上传此图像。我用改造来做到这一点。 我按照ImagePicker课程将图像收集到我的Imageview中。
收集图片代码:
CircleImageView imageViewProfile_Picture;
public void onPickImage(View view) {
Intent chooseImageIntent = ImagePicker.getPickImageIntent(this);
startActivityForResult(chooseImageIntent, PICK_IMAGE_ID);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case PICK_IMAGE_ID:
Bitmap bitmap = ImagePicker.getImageFromResult(this, resultCode, data);
// TODO collect picture and show in Imageview
imageViewProfile_Picture.setImageBitmap(bitmap);
break;
default:
super.onActivityResult(requestCode, resultCode, data);
break;
}
}
要将图像上传到服务器,请使用Retrofit库
// How to get the file object from bitmap
File file=null;
RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("upload", file.getName(), reqFile);
API接口
@Multipart
@POST("/taxiapi/api?")
Call<SignUpResponse> signup(@Part("user_name") RequestBody user_name,
@Part("name") RequestBody name,
@Part("password") RequestBody password,
@Part("action") RequestBody action,
@Part("email") RequestBody email,
@Part("phone") RequestBody phone,
@Part("icard") RequestBody icard,
@Part MultipartBody.Part image,
@Part("profile_picture") RequestBody pic_name);
我的问题是如何从结果意图中设置Imageview以在服务器中上传所选图片后从这种情况获取文件对象?
答案 0 :(得分:2)
尝试使用此
File file = new File(photoPath);
photoPath将包含相机拍摄的最后一张图像的路径。
然后PLAN_FINDING_USE = [:show]
include Concerns::V1::PlanFinding