你好,我正在使用改造上传图像文件。 任何一个可以有想法如何如何使用Retrofit在android中传递图像?
-1
A
回答
1
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;
}
步骤:: 1PASS的文件路径,它会回报你多体
@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:通过像这样
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:并通过参数在你的服务类
3
传给你需要传递mulitypart对象改装:
MultipartBody.Part carImage = null;
if (!TextUtils.isEmpty(imagePath)) {
File file = FileUtils.getFile(getContext(), imagePath);
// create RequestBody instance from file
final RequestBody requestFile =
RequestBody.create(MediaType.parse("multipart/form-data"), file);
// MultipartBody.Part is used to send also the actual file name
carImage = MultipartBody.Part.createFormData("image", file.getName(), requestFile);
}
1
请通过以下link。
1
0
第1步:首先初始化服务类
public interface ImageUploadService {
@Multipart
@POST("upload")
Call<ResponseBody> upload(
@Part("description") RequestBody description,
@Part MultipartBody.Part file
);
}
第2步:在下一步使用本你想要上传图片或文件
private void uploadFile(Uri fileUri) {
// create upload service client
FileUploadService service =
ServiceGenerator.createService(ImageUploadService.class);
// https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java
// use the FileUtils to get the actual file by uri
File file = FileUtils.getFile(this, fileUri);
// create RequestBody instance from file
RequestBody requestFile =
RequestBody.create(
MediaType.parse(getContentResolver().getType(fileUri)),
file
);
// MultipartBody.Part is used to send also the actual file name
MultipartBody.Part body =
MultipartBody.Part.createFormData("picture", file.getName(), requestFile);
// add another part within the multipart request
String descriptionString = "hello, this is description speaking";
RequestBody description =
RequestBody.create(
okhttp3.MultipartBody.FORM, descriptionString);
// finally, execute the request
Call<ResponseBody> call = service.upload(description, body);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call,
Response<ResponseBody> response) {
Log.v("Upload", "success");
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e("Upload error:", t.getMessage());
}
});
}
步骤3:你可以使用这样
uploadFile(Uri.fromFile(new File("/sdcard/cats.jpg")));
在您的活动。
最后一步:你需要添加
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
在清单在additon与
<uses-permission android:name="android.permission.INTERNET"/>
参考this。
您可以上传任何类型的文件。
0
/* Create interface like below. */
public interface uploadWishImage {
@Multipart
@POST("upload/image")
Call<JsonObject> postImage(@Part MultipartBody.Part image, @Part("name") RequestBody name);
}
/* image upload code */
File file = new File("here your file path");
RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), reqFile);
RequestBody name = RequestBody.create(MediaType.parse("text/plain"), "file");
uploadWishImage postService = RetrofitApi.makeNetworkRequestWithHeaders(AddWish.this).create(uploadWishImage.class);
Call<JsonObject> call = postService.postImage(body, name);
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
// somethings to do with reponse
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
// Log error here since request failed
}
});
相关问题
- 1. 如何使用Retrofit Android上传图片?
- 2. Android Retrofit上传图像文件使用多部分请求
- 3. 如何仅使用Retrofit 2.0上传图像
- 4. Android,Retrofit如何使用@Headers?
- 5. 在Android中传递PutExtra中的图像
- 6. 在Android中使用Retrofit 2.0
- 7. 如何使用PInvoke传递图像
- 8. 如何使用Retrofit 2.3在JSONObject中发送图像文件?
- 9. 如何在Android上使用GraphQL和Retrofit?
- 10. 如何在SFML中传递图像C++
- 11. 如何在android中使用retrofit 2上传JSON中的多部分图像数据?
- 12. 如何在Android中的活动之间传递图像?
- 13. 在Android Retrofit库中传递值到URL的差异
- 14. Retrofit分段上传图像失败
- 15. 如何在Retrofit 2.0中传递动态标题授权?
- 16. 在C中传递图像#
- 17. Android Retrofit 2.0使用图像,视频和音频上传与MultipartBody.Par方法
- 18. Android - 如何使用Retrofit SimpleXML解析xml?
- 19. 如何在Android中使用图像?
- 20. 如何在android中使用gif图像?
- 21. Retrofit 2.0.2图像上传使用多部分
- 22. Android Retrofit 2上传多个图像到服务器
- 23. 在Android中使用Retrofit退出
- 24. 如何在使用'kubectl run'命令时传递图像拉密?
- 25. 如何在android中使用retrofit解析此web服务?
- 26. 如何在Android POST方法中使用Retrofit以及body?
- 27. 如何上传图像采用了android
- 28. 如何在android上使用ftp上传图像
- 29. Android Retrofit简单使用
- 30. 使用“argv”传递图像文件C++
有任何一个问题? –