如何使用Android中的改造将图像对象发送到服务器

时间:2019-04-12 09:29:40

标签: java android retrofit

我正在使用改造功能更新我的个人资料图片。我必须发送图像对象和当前登录用户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

但是图像未更新...我已经在邮政人员上尝试过api,它正在正确更新配置文件图片...请告诉我问题是什么,我该如何解决

这是我的api通话。

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

我正在添加从邮递员那里获取的收益。 this is the locked response of post man

2 个答案:

答案 0 :(得分:2)

尝试使用@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

答案 1 :(得分:0)

请在行

下更新
import {PlatformLocation } from '@angular/common';
export class ConfirmationComponent implements OnInit {
  public base_url;
   constructor(private platformlocation: PlatformLocation){
    this.base_url = location.origin + this.platformlocation.getBaseHrefFromDOM();
   }
}

代替此

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

并确保使用@part代替此@Query

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