如何使用Retrofit在android中传递图片? [英] How to Pass the image in android using Retrofit?

查看:36
本文介绍了如何使用Retrofit在android中传递图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在使用改造功能来上传图片文件.任何人都可以知道如何通过

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

推荐答案

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 :: 1传递文件路径,它将返回您的MultiPart主体

}

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

@Multipart
@POST(Endpoint.POST_URL)
Call<DecisionStepThirdResponse> 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);

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

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

第2步:像这样传递请求

Step 2: Pass the Request like this

 public void uploadUserProfile(UpdateImageRequest request, MultipartBody.Part file, Callback<UpdateImageResponse> 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);
}

第3步:并在您的Serviceclass中传递参数

Step 3 : And Pass the Parameter in your Serviceclass

这篇关于如何使用Retrofit在android中传递图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆