0

Getting " open failed: ENOENT (No such file or directory) " error while selecting file from android default file chooser. I am developing an app that selects file from external storage and upload it to server using retrofit api. API is working fine in postman and I am getting file location (toast). i have added permission of READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE and MANAGE_EXTERNAL_STORAGE here is my code-- selecting file

    private void selectResumeFromFileManager(){
            if(ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
                ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);
            }
            else{
                Intent intent=new Intent(Intent.ACTION_OPEN_DOCUMENT);
    //        intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("*/*");
                intent=Intent.createChooser(intent,"Choose a file");
                startActivityForResult(intent,RESUME_CODE);
            }
        }

@Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && requestCode == ImagePicker.REQUEST_CODE) {
            Uri uri=data.getData();
            file=new File(uri.getPath());
//            mytext.setText(file.getName());
            binding.imgUser.setImageURI(data.getData());
            updateImage();

        }else if(resultCode==Activity.RESULT_OK && requestCode==RESUME_CODE){
            Uri uri=data.getData();
            Toast.makeText(getContext(), uri.getPath(), Toast.LENGTH_SHORT).show();
            Log.d("fileResult","storage/emulated/0/"+uri.getPath());
            resume=new File(uri.getPath());
            updateResume();
        }
    }

here is the code to upload the resume to server.

protected void updateResume(){
    RequestBody requestBodyUserId=RequestBody.create(MediaType.parse("multipart/form-data"),userModel.getUserId().toString());
    if(resume!=null){
        RequestBody requestBodyResume=RequestBody.create(MediaType.parse("multipart/form-data"),resume.getAbsoluteFile());
        MultipartBody.Part requestResume=MultipartBody.Part.createFormData("resume",resume.getName(),requestBodyResume);
        Call<StringResponseModel> call=ApiController.getInstance()
                .getApi()
                .updateResume(requestResume,requestBodyUserId);
        call.enqueue(new Callback<StringResponseModel>() {
            @Override
            public void onResponse(Call<StringResponseModel> call, Response<StringResponseModel> response) {
                if(response.isSuccessful()){
                    StringResponseModel responseBody=response.body();
                    if(responseBody.getError()){
                        Toast.makeText(getContext(),"resume response is unsuccessful "+response.code(),Toast.LENGTH_LONG).show();
                        Log.d("resresponseUnsucc",response.code()+" ");
                    }else{
                        Toast.makeText(getContext(),"resume response is successful "+response.code(),Toast.LENGTH_LONG).show();
                        Log.d("resresponseSucc",response.code()+" ");
                    }

                }else{
                    Toast.makeText(getContext(),"resume response is unsuccessfull "+response.code(),Toast.LENGTH_LONG).show();
                    Log.d("resresponseUnsucc",response.code()+" ");
                }
            }

            @Override
            public void onFailure(Call<StringResponseModel> call, Throwable t) {
                Toast.makeText(getContext(),"unsuccessfull "+t.getMessage(),Toast.LENGTH_LONG).show();
                Log.d("Unsucc",t.getMessage());
            }
        });
    }else{
        Toast.makeText(getContext(), "Please select a resume", Toast.LENGTH_SHORT).show();
    }
}

this is my retrofit interface method

@Multipart
    @POST("update_resume.php")
    Call<StringResponseModel> updateResume(@Part MultipartBody.Part resume, @Part("user_id") RequestBody user_id);
Rohit Maurya
  • 1
  • 1
  • 3
  • `while selecting file from android default file chooser. ` Sorry, you mean: while using ACTION_OPEN_DOCUMENT. – blackapps May 28 '22 at 09:01
  • `. i have added permission of READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE and MANAGE_EXTERNAL_STORAGE` You do not need any of those permissions using ACTION_OPEN_DOCUMENT. – blackapps May 28 '22 at 09:03
  • `resume=new File(uri.getPath());` That code makes no sense. You cannot use the File class for that 'path' as it is no path. Look at its value. It has nothing to do with a path on the file system. You could have tested that with using resume.exists() and resume.canRead(). – blackapps May 28 '22 at 09:04
  • `Log.d("fileResult","storage/emulated/0/"+uri.getPath());` No. No. That makes no sense. – blackapps May 28 '22 at 09:06
  • `Toast.makeText(getContext(), uri.getPath(), Toast.LENGTH_SHORT).show();` Why didn't you tell what you saw? – blackapps May 28 '22 at 09:06
  • i am getting the file location correctly (toast) i.e. "document/primary:Download/resume.pdf" – Rohit Maurya May 28 '22 at 09:18
  • No. That is no valid location. Better Toast uri.toString() instead to see the nice content scheme you obtained. Use the uri itself. Use that content scheme. – blackapps May 28 '22 at 09:23
  • thank you. Now how can I solve this problem? Please help me :( – Rohit Maurya May 28 '22 at 10:59
  • A `Uri` is not a file. Create a `RequestBody` that uses the `Uri` itself. The accepted answer on the duplicate question points to three separate implementations of such a `RequestBody`. The second code snippet from [this OkHttp issue comment](https://github.com/square/okhttp/issues/3585#issuecomment-327319196) is in Java, while the other two are in Kotlin. – CommonsWare May 28 '22 at 11:49

0 Answers0