0

I am trying to upload an image on server with the help of retrofit but I getting an error. The problem comes in when I do enqueue to the service call. In the onFailure I get this error:

content:/com.android.providers.media.documents/document/image%3A150664(No such file or directory).

However, This is the uri and file path which I get in the onActivityResult:

uri: content://com.android.providers.media.documents/document/image%3A150664

This is Request

@Multipart
@POST("Travel/Upload")
Call<RequestBody> uploadImage(@Part("description") RequestBody description,@Part 
    MultipartBody.Partphoto);

My Code

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        textdesc=findViewById(R.id.descriptionn);
        image1=findViewById(R.id.imgshow);
        btnChoose=findViewById(R.id.btnbrowse);
        btnUpload=findViewById(R.id.btnUpload);

        btnChoose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                choose();
            }
        });

        btnUpload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // uploadImage(bitmap);
                uploadImage();
            }
        });

        progressDialog=new ProgressDialog(MainActivity.this);
        progressDialog.setMessage("Image uploaded.....");

    }

    private void choose() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);

        startActivityForResult(Intent.createChooser(intent, "select Image"), 100);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 100 && resultCode == RESULT_OK && data != null && data.getData() != null) {
            filePath = data.getData();
            imagepath=String.valueOf(filePath);
            Toast.makeText(MainActivity.this, ""+imagepath, Toast.LENGTH_SHORT).show();
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
                image1.setImageBitmap(bitmap);
               // Toast.makeText(MainActivity.this, ""+filePath, Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                e.printStackTrace();
            }


        }
    }
     private void uploadImage()
    {
        //imagepath=getRealPathFromURI(filePath);
        //Toast.makeText(MainActivity.this, ""+imagepath, Toast.LENGTH_SHORT).show();
        File file=new File(imagepath);
        Retrofit retrofit=ResponseClass.getRetrofit();
        RequestBody requestBody=RequestBody.create(MediaType.parse("multipart/form-data"),file);
        MultipartBody.Part photo=MultipartBody.Part.createFormData("photo",file.getName(),requestBody);

        RequestBody description=RequestBody.create(MediaType.parse("text/plain"),"This is new Image");

        ourRetrofit ourRetrofitt=retrofit.create(ourRetrofit.class);
        Call call=ourRetrofitt.uploadImage(description,photo);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFailure(Call call, Throwable t) {
                Toast.makeText(getApplicationContext(),t.getMessage(),Toast.LENGTH_LONG).show();
            }
        });
    }
   }
Kishore Jethava
  • 6,666
  • 5
  • 35
  • 51

0 Answers0