Image path says cannot read even with permissions given (Android Studio) java-ThrowExceptions

Exception or error:

I am new to Android and I am trying to get the Uri of an image that a user selects from the downloads folder. Currently, I am able to open the downloads folder and select an image. However, when I take the path of the image selected, file.canRead() returns false.

These are the permissions that I have provided for the app

    int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
    int permission2 = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
    if (permission != PackageManager.PERMISSION_GRANTED && permission2 != PackageManager.PERMISSION_GRANTED ) {
            ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
            ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);

Here is where I am taking the image that I selected from the download folder and then passed the path to a new file:

        selectedImage = imageReturnedIntent.getData();
        String path = getRealPathFromURI(getApplicationContext(), selectedImage);
        File selPath = new File(path);

        Boolean b5 = selPath.isFile();
        Boolean b1 = selPath.canRead();
        Boolean b2 = selPath.exists();

isFile() returns true

exists() returns true

canRead() returns false, and I don’t know why because I have given permissions.

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *