使用改造2发送图像文件 [英] Sending image file with retrofit 2
问题描述
我正在尝试使用Retrofit 2将图像上传到我们项目的服务器
I am trying to upload an image to our project's server using Retrofit 2
图像是通过图像拾取活动选取的,并且似乎自文件起作用(图像)可以使用Picasso显示。
The image is picked through an image picking activity and seems to work since the file (image) can be displayed using Picasso.
改造成功但是服务器似乎没有得到文件。
这是服务器端部分。
Retrofit succeeds however the server doesn't seem to get the file. Here is the server side part.
func (c *gin.Context) {
file, header , err := c.Request.FormFile("profileImage")
// err = http: no such file
}
甚至RequestBody在我测试时也会打印出连贯的信息(尺寸,图像类型......)
Even the RequestBody prints coherent information when I tested it (size, image type...)
服务:
@Multipart
@PATCH("/user/profileImage")
Call<ResponseBody> modifyUserImage(@Part("profileImage") RequestBody profileImage, @Part("userID") RequestBody userID);
以下代码是同一片段类的一部分
Following code is part of the same Fragment class
打开图片拣选活动:
Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
getIntent.setType("image/*");
Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
Intent chooserIntent = Intent.createChooser(getIntent, "Select Image");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{pickIntent});
startActivityForResult(chooserIntent, 1);
关于活动结果:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == 1) {
// process the result
Uri selectedImage = data.getData();
String wholeID = DocumentsContract.getDocumentId(selectedImage);
String id = wholeID.split(":")[1];
String[] column = {MediaStore.Images.Media.DATA};
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = getActivity().getContentResolver().
query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{id}, null);
String filePath = "";
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
file = new File(filePath);
Picasso
.with(getActivity().getApplicationContext())
.load(file)
.into(civ_userProfilePicture);
}
}
请求
Call<ResponseBody> call = ServiceSingelton.getmInstance().getService()
.modifyUserImage(RequestBody.create(MediaType.parse("image/*"), file),
RequestBody.create(MediaType.parse("text/plain"), ServiceSingelton.getmInstance().getUserID()));
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.code() == 200) {
Log.d("RETROFIT SUCCESS", "Pic should be sent");
} else {
Log.d("RETROFIT SUCCESS", "Error code received modifying user");
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.d("RETROFIT ERROR", t.getMessage());
}
});
推荐答案
有人给了我这个修复工作:
Someone gave me this fix which worked :
要发布带文件名的部分,您应该将@Bart(profileImage)
RequestBody profileImage更改为@Part RequestBody profileImage,并传递
它是MultipartBody.Part.createFormData(partName,filename,requestBody):
To post a part with filename, you should change @Part("profileImage") RequestBody profileImage to @Part RequestBody profileImage, and pass it MultipartBody.Part.createFormData(partName, filename, requestBody):
// Service
@Multipart
@PATCH("/user/profileImage")
Call<ResponseBody> modifyUserImage(@Part MultipartBody.Part profileImage, @Part("userID") RequestBody userID);
// Call
MultipartBody.Part imagePart = MultipartBody.Part.createFormData("profileImage", file.getName(), RequestBody.create(MediaType.parse("image/*"), file));
Call<ResponseBody> call = ServiceSingelton.getmInstance().getService()
.modifyUserImage(imagePart,
RequestBody.create(MediaType.parse("text/plain"), ServiceSingelton.getmInstance().getUserID()));
这篇关于使用改造2发送图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!