I am trying to upload image using retrofit 2.0.
I followed this guide and I use the function uploadFile. I downloaded the FileUtilis.java from link like says on the code for obtain a File form Uri.
private void uploadFile(Uri fileUri) {
// create upload service client
FileUploadService service =
ServiceGenerator.createService(FileUploadService.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("multipart/form-data"), 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(
MediaType.parse("multipart/form-data"), 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());
}
});
}
When I execute the program, the function go to callback onResponse and I can read the response of body from my server, but the image that I upload is blank!!
The fileUri that pass of the funtion uploadFile is an image from camera/gallery, and so I have a button imageProfile and when I push the button I launch the intent like so
imageProfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
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, getResources().getString(R.string.select_image_str));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{pickIntent});
startActivityForResult(chooserIntent, PICK_IMAGE);
}
});
and in the onActivityResult I put my Uri inside variable uriImageProfile like this
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK) {
if (data == null) {
//Display an error
return;
}
try {
uriImageProfile = data.getData();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
The uploadFile method is called in my code like this and is similar but not so equals from original. I have a class UserControllerApi in which I have all methods associated to API of my server, and so after creation I call the mehod uploadPictureProfile
UserControllerApi userControllerApi = new UserControllerApi();
userControllerApi.uploadPictureProfile(idUser, uriImageProfile, this);
Thanks