4

I tried to upload an image using Retrofit but I am getting this error:

Unable to submit post to API: java.io.FileNotFoundException: /document/image:30231: open failed: ENOENT (No such file or directory)

My interface is like this:

public interface MyService{
    @Multipart
    @POST("/url")
    Call<ResponseBody> addNewEvent( @Part("case_Id") int caseId,@Part MultipartBody.Part(file);
}

On Button click, selectImage() function is called:

private void selectImage() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Please select image",1);
}

In the onActivityResult part, I did the following:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1 && resultCode!=RESULT_CANCELED && resultCode == RESULT_OK && data != null && data.getData() != null) {
        FilePathUri = data.getData();
        doAddNewEvent();
    }
}

From above, doAddNewEvent() function is called:

public void doAddNewEvent() {
    File file = new File(FilePathuri.getPath());
    RequestBody requestFile=RequestBody.create(MediaType.parse("multipart/form-data"), file);
    MultipartBody.Part body = MultipartBody.Part.createFormData("image", file.getName(), requestFile);

 apiService.addNewEvent(inputCaseId, body).enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            if (response.isSuccessful()) {                    
                ResponseBody addEventResponse = response.body();
                Log.d("as", "response: " + addEventResponse);
                finish();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Log.e("as", "Unable to submit post to API");
        }
    });
}
sangeeta
  • 158
  • 4
  • 14
  • There is some problem in your `getPath()` debug your code. Check [This discussion](https://stackoverflow.com/questions/7305504/convert-file-uri-to-content-uri) or you can also search for similar . – ADM Feb 12 '18 at 07:56
  • On `FilePathUri.getPath()`, I get this: `/document/image:23165` – sangeeta Feb 12 '18 at 08:03
  • `java.io.FileNotFoundException: /document/image:30231:`. Indeed that is no valid path on the file system. Thats a part of a content scheme. The complete scheme is given by FilePathUri.toString(). – greenapps Feb 12 '18 at 08:26
  • Is there any way that I can get the right path? – sangeeta Feb 12 '18 at 08:28
  • You should ask: is there any way that i can retrofit let use the obtained uri directly? Or the content scheme? Can you let retrofit upload files from a stream? From an InputStream? – greenapps Feb 12 '18 at 08:29

1 Answers1

2

There is a problem to get file path

Try with this :

if (requestCode == 1 && resultCode!=RESULT_CANCELED && resultCode == RESULT_OK && data != null && data.getData() != null) {

            FilePathStr = null;

            if (data != null) {



                Uri selectedImage = data.getData();
                String[] filePath = {MediaStore.Images.Media.DATA};
                Cursor c = getContentResolver().query(selectedImage, filePath,
                        null, null, null);
                c.moveToFirst();
                int columnIndex = c.getColumnIndex(filePath[0]);
                FilePathStr = c.getString(columnIndex);
                c.close();
                doAddNewEvent();




            }

        }

and make multipart using string path

MultipartBody.Part body = null;

            if (FilePathStr != null) {
                File file = new File(FilePathStr );
                RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
                body = MultipartBody.Part.createFormData("image", file.getName(), reqFile);

            }
Adil
  • 812
  • 1
  • 9
  • 29