1

I am accessing pictures of the device's gallery via my app, when the picture is accessed the metadata of the picture will be read and stored in metadata. The problem I'm facing is that whenever the program tries to read the metadata I'm getting the following error java.io.FileNotFoundException: /storage/emulated/0/Snapchat/Snapchat-1185425082.jpg (Permission denied).

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

    if(resultCode == RESULT_OK && requestCode == PICK_IMAGE){

        imageUri = data.getData();
        imageView.setImageURI(imageUri);
        File picturesfile = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        picturesfile.setReadable(true);
        picturesfile.setExecutable(true);




        String[] projection = {MediaStore.Images.Media.DATA};

        try {
            Cursor cursor = getContentResolver().query(imageUri, projection, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(projection[0]);
            path = cursor.getString(columnIndex);


            Log.d("Picture Path", path);

        }
        catch(Exception e) {
            Log.e("Path Error", e.toString());
        }

        File jpegFile = new File(path);
        jpegFile.setReadable(true);
        jpegFile.setExecutable(true);

        try {
             metadata =  ImageMetadataReader.readMetadata(jpegFile);
            for (Directory directory : metadata.getDirectories()) {
                for (Tag hoi : directory.getTags()) {
                    Log.d("tags ", hoi.toString());
                }
            }


        } catch (ImageProcessingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }






    }
    if(resultCode == RESULT_OK && requestCode == 0){
        Bitmap bitmap = (Bitmap)data.getExtras().get("data");
        imageView.setImageBitmap(bitmap);
    }

}
4

1 回答 1

3

从战术上讲,您似乎没有READ_EXTERNAL_STORAGE权限,您需要在清单和运行时请求该权限。

除此之外:

  • 您无需query()返回DATA

  • 不要求DATA列有值

  • 不要求该DATA列具有文件系统路径

  • 不要求DATA列中的文件系统路径是您可以访问的文件,即使使用READ_EXTERNAL_STORAGE

特别是,可以保证您的代码在 Android Q 上会失败,而且对于许多其他设备上的许多用户来说,它也很可能会失败。

使用Uri( imageUri)ContentResolver来获取一个InputStream(或者可能是一个FileDescriptor)传递给您的库。

于 2019-05-12T20:45:24.723 回答