[英]Upload bitmap image to server with retrofit
从用户首选的应用(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;
}
}
要将图像上传到服务器,我使用翻新库
// 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在服务器中上传所选图片后,如何从这种情况下获取文件对象?
尝试使用这个
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToLast();
String photoPath = cursor.getString(column_index_data);
photoPath将包含相机拍摄的最后一张图像的路径。
然后File file = new File(photoPath);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.