0

I have read in many answers at SO that File Function is not recommended any more to create a file from onActivityResults and the best way is to use the InputStream and getContentResolver. I read a lot but I really could not make it it work with the below code (with File Class Method) using the class from @Yuriy Kolbasinskiy. at this link

What am I doing wrong?

With the emulator running Android 8, when I select the file from Download tap, it opens, but when I get the file from the images tap it gives the error.

From my actual phone Galaxy Note 10 and Android 10, it shows the error from all location and options.

The error "open failed: ENOENT (No such file or directory)"

The Button to call ACTION_GET_CONTENT

mBtnAddphoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (!hasPermissions(getActivity(), PERMISSIONS)) {
                ActivityCompat.requestPermissions(getActivity(), PERMISSIONS, PERMISSION_ALL);
            } else {

                Intent intGetFile = new Intent(Intent.ACTION_GET_CONTENT);
                intGetFile.setType("image/*");
                startActivityForResult(intGetFile, 1983);

            }
        }
    });

The onActivityResult code.

@Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK)
            switch (requestCode) {
                case 1983:
                    Uri uriSelectedImage = data.getData();
                    String imgFullPath = uriSelectedImage.getPath();
                    String imgPath = imgFullPath.substring(imgFullPath.lastIndexOf(":") + 1);
                    File imgFile = new File(imgPath);
                    String mimeType = getContext().getContentResolver().getType(uriSelectedImage);

                    mBtnAddphoto.setEnabled(false);
                    mProgressBar = mNewadFragView.findViewById(R.id.progressBar);
                    ProgressRequestBody fileBody = new ProgressRequestBody(imgFile, mimeType, this);
                    MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", imgFile.getName(), fileBody);
                    Call<RetroMedia> uploadMediaCall = mWordPressApi.uploadMedia(filePart, mUserToken);
                    uploadMediaCall.enqueue(new Callback<RetroMedia>() {
                        @Override
                        public void onResponse(Call<RetroMedia> call, Response<RetroMedia> response) {
                            if (!response.isSuccessful()) {
                                String strmsg = "\n Something is wrong! Please contact adming";
                                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                                builder.setMessage("Code: " + response.code() + strmsg)
                                        .setCancelable(false)
                                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int id) {
                                                //do things
                                            }
                                        });
                                AlertDialog alert = builder.create();
                                alert.show();
                                return;
                            }
                            mIntFeaturedImage = response.body().getId();
                            mEdtxtDescription.fromHtml("<img src=\"" + response.body().getRetroMediaDetails().getSizes().getMediumLarge().getSourceUrl() + "\"> <br><br>");
                            onFinish();
                        }
                        @Override
                        public void onFailure(Call<RetroMedia> call, Throwable t) {
                            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                            builder.setMessage(t.getMessage())
                                    .setCancelable(false)
                                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int id) {
                                            //do things
                                            onError();
                                        }
                                    });
                            AlertDialog alert = builder.create();
                            alert.show();
                        }
                    });
            }
    }
Abdullah
  • 624
  • 6
  • 15
  • 1
    Get rid of `imgFullPath`, `imgPath`, and `imgFile`. Use [a custom `RequestBody` to work with an `InputStream`](https://github.com/square/okhttp/issues/3585#issuecomment-327319196). – CommonsWare Apr 15 '20 at 17:51
  • Thank you very much, this has solved the problem of ENOENT. Really Appreciated. – Abdullah Apr 15 '20 at 18:55
  • Now, I can select all files without a problem with the customer RequestBody, all files are rejected by server (WordPress) "This file is not permitted for security reasons". – Abdullah Apr 15 '20 at 19:47
  • I hope @Yuriy-Kolbasinskiy can get involve to customize the custom RequestBody to make it work with his progress bar. – Abdullah Apr 16 '20 at 07:13

0 Answers0