gpt4 book ai didi

java - 如何在android中使用retrofit将图像对象发送到服务器

转载 作者:行者123 更新时间:2023-12-02 01:33:30 25 4
gpt4 key购买 nike

我正在使用改造更新我的个人资料图片。我必须发送图像对象和当前登录用户 ID,这是我的代码...

private void uploadProfileImage(){
uid = DatabaseUtil.getInstance().getUser().getProfile().getUmeta().getId();
mRegProgress.setTitle("Updating profile Image");
mRegProgress.setMessage("Please wait...");
mRegProgress.setCanceledOnTouchOutside(false);
mRegProgress.show();

File profile_image_file = new File(mediaPath);

RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), profile_image_file);
MultipartBody.Part profile_image = MultipartBody.Part.createFormData("file", profile_image_file.getName(), requestBody);

Call<ResponseBody> call = RetrofitClient.getInstance().getApi().uploadProfile(uid , profile_image);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

if (response.code() == 200){
mRegProgress.hide();
String s = response.body().toString();
pDialog = new SweetAlertDialog(getActivity(), SweetAlertDialog.SUCCESS_TYPE);
pDialog.setTitleText("Good job!");
pDialog.setContentText("Profile image successfully!");
pDialog.show();
}else if (response.code() == 203){
Toast.makeText(getActivity() , "Image upload Error" , Toast.LENGTH_SHORT).show();

}
}

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
t.printStackTrace();
}
});
}

我也在使用这个代码......

case SELECT_PROFILE_PIC:
if (resultCode == RESULT_OK) {


// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};

Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
assert cursor != null;
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
mediaPath = cursor.getString(columnIndex);

// Set the Image in ImageView for Previewing the Media
dd_profile_view.setImageBitmap(BitmapFactory.decodeFile(mediaPath));
cursor.close();

uploadProfileImage();
}
break;

this is what i am sending in my code

但是图像没有更新...我已经在 postman 上尝试了 api,它正在正确更新个人资料图片...请告诉我问题是什么以及如何解决它谢谢

这是我的 api 调用..

@Multipart
@POST("media/upload-media")
Call<ResponseBody> uploadProfile(
@Query("id") String id,
@Part MultipartBody.Part profile_image
);

我正在添加从 postman 那里获得的reproce.. this is the locked response of post man

最佳答案

尝试使用@Part代替@Query

@Multipart
@POST("media/upload-media")
Call<ResponseBody> uploadProfile(
@Part("id") RequestBody id,
@Part MultipartBody.Part profile_image
);

替换

RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), profile_image_file);

与:

RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), profile_image_file);

您可以点击此链接获取完整说明enter link description here

关于java - 如何在android中使用retrofit将图像对象发送到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55648562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com