How to Pass the image in android using Retrofit?

后端 未结 6 612
一向
一向 2021-01-17 06:03

Hello i am working on upload image file using retrofit. Can any one have idea how to pass in

6条回答
  •  既然无缘
    2021-01-17 06:37

    public static MultipartBody.Part UploadImage(String filePath,String param) {
    
       MultipartBody.Part body = null;
        try {
            body = MultipartBody.Part.createFormData("", "", null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //profileUpdateRequest.setWebsite(lblWebsite.getText().toString().trim());
        if ((!filePath.equals(""))) {
            File file = new File(filePath);
            RequestBody photo = RequestBody.create(MediaType.parse("image/*"), file);
            body = MultipartBody.Part.createFormData(param, file.getName(), photo);
        }
        return body;
    

    }

    Step::1Pass the file Path and it will return you MultiPart body

    @Multipart
    @POST(Endpoint.POST_URL)
    Call uploadUserProfile(@Part("api_id") RequestBody api_id,
                                                    @Part("api_secret") RequestBody api_secret,
                                                    @Part("api_request") RequestBody api_request,
                                                    @Part("data") RequestBody data,
                                                    @Part MultipartBody.Part profile_image);
    

    ========================

    Step 2: Pass the Request like this

     public void uploadUserProfile(UpdateImageRequest request, MultipartBody.Part file, Callback callback) {
        String api_request = "uploadUserProfile";
        String data = new Gson().toJson(request);
        IRoidAppHelper.Log("application_form_permission", data);
        json().uploadUserProfile(
                RequestBody.create(MediaType.parse("text/plain"), api_id),
                RequestBody.create(MediaType.parse("text/plain"), api_secret),
                RequestBody.create(MediaType.parse("text/plain"), api_request),
                RequestBody.create(MediaType.parse("text/plain"), data)
                , file).enqueue(callback);
    }
    

    Step 3 : And Pass the Parameter in your Serviceclass

提交回复
热议问题