Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image path表示,即使具有给定的权限(Android Studio)java也无法读取_Java_Android_Permissions - Fatal编程技术网

Image path表示,即使具有给定的权限(Android Studio)java也无法读取

Image path表示,即使具有给定的权限(Android Studio)java也无法读取,java,android,permissions,Java,Android,Permissions,我是Android新手,我正在尝试获取用户从下载文件夹中选择的图像的Uri。目前,我可以打开下载文件夹并选择图像。但是,当我选择所选图像的路径时,file.canRead返回false 这些是我为应用程序提供的权限 int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE); int permission2 = ActivityCompat

我是Android新手,我正在尝试获取用户从下载文件夹中选择的图像的Uri。目前,我可以打开下载文件夹并选择图像。但是,当我选择所选图像的路径时,file.canRead返回false

这些是我为应用程序提供的权限

    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);
    }
这里是我从下载文件夹中选择的图像,然后将路径传递到新文件的位置:

        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返回true

exists返回true


canRead返回false,我不知道为什么,因为我已经授予了权限。

getRealPathFromURI肯定是错误的。删除它。使用ContentResolver和openInputStream读入代码段中Uri SelecteImage标识的内容。@Commonware感谢您的快速回复。起初,我没有使用getRealPathFromURI,对于isFile,我仍然得到False。因此,我试图找到真正的路径,看看它是否有帮助。没有。我需要将所选文件传递给另一个方法,以便将其与另一个图像进行比较。我在访问文件本身时遇到了问题..我仍然对isFile有误解-停止用文件来思考。您没有处理文件。您正在处理由Uri标识的内容。我不知道您计划如何将此图像与另一图像进行比较,但BitmapFactory可以从InputStream读取位图。您可以使用ContentResolver在Uri标识的内容上获取InputStream,并且openInputStream.getRealPathFromURI保证是错误的。删除它。使用ContentResolver和openInputStream读入代码段中Uri SelecteImage标识的内容。@Commonware感谢您的快速回复。起初,我没有使用getRealPathFromURI,对于isFile,我仍然得到False。因此,我试图找到真正的路径,看看它是否有帮助。没有。我需要将所选文件传递给另一个方法,以便将其与另一个图像进行比较。我在访问文件本身时遇到了问题..我仍然对isFile有误解-停止用文件来思考。您没有处理文件。您正在处理由Uri标识的内容。我不知道您计划如何将此图像与另一图像进行比较,但BitmapFactory可以从InputStream读取位图。您可以使用ContentResolver和openInputStream获取Uri标识的内容的InputStream。