1

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

Luisa Repele
  • 165
  • 1
  • 1
  • 8
  • from where you call uploadFile() method? – Nisarg Jul 07 '16 at 10:16
  • I call uploadFile() on my activity after the execution on onActivityResult – Luisa Repele Jul 07 '16 at 10:23
  • can you please post it ? – Nisarg Jul 07 '16 at 10:24
  • just check uri you sending proper or not otherwise code looks ok and might be issue from server side !!! – Nisarg Jul 07 '16 at 10:52
  • do you get any logcat error? – Nisarg Jul 07 '16 at 10:56
  • I suppose that the problem is relative at multipart request.. The upload image API is formalized with a body type File not Form (multipart). Server side this API works correctly because I implemented the upload of image again in iOS app version (I use Alamofire). Do you have some example in which the request server is formalized this? – Luisa Repele Jul 07 '16 at 12:21
  • yes right I never did I have done it using byte[] or base64 and well i found [this](http://stackoverflow.com/a/37072667/3117966) might help – Nisarg Jul 07 '16 at 12:25
  • I try this solution using Map but not works – Luisa Repele Jul 07 '16 at 12:46
  • ok try solution of [this](http://stackoverflow.com/a/36657336/3117966) and [this](http://stackoverflow.com/a/37818515/3117966) and [this](http://stackoverflow.com/a/33490216/3117966) – Nisarg Jul 07 '16 at 12:51
  • did it work or not? – Nisarg Jul 07 '16 at 13:09
  • The first and second link not work. I am trying the 3rd link but I cannot found MultipartBuilder() – Luisa Repele Jul 07 '16 at 13:12
  • well I am doing R&D for your solution i never used this but will use in future and try [this](http://stackoverflow.com/q/36088444/3117966) – Nisarg Jul 07 '16 at 13:19
  • Ok I solved my issues. I implemented [link] (http://stackoverflow.com/questions/33482385/how-to-upload-a-photo-using-retrofit/33490216#33490216) but insted pass to services **body** I pass **photo** variable and now works :-) – Luisa Repele Jul 07 '16 at 13:27

0 Answers0