java图片路径禁止访问权限,尝试从图片读取元数据时获取FileNotFoundException(权限被拒绝)...

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);

}

}

解决方案

Tactically, it would appear that you do not have the READ_EXTERNAL_STORAGE permission, which you need to request in the manifest and at runtime.

Beyond that:

There is no requirement for your query() to return a DATA column

There is no requirement that the DATA column have a value

There is no requirement that the DATA column have a filesystem path

There is no requirement that the filesystem path in the DATA column be a file that you can access, even with READ_EXTERNAL_STORAGE

In particular, it is guaranteed that your code will fail on Android Q, and it is very likely to fail for lots of users on lots of other devices as well.

Use the Uri (imageUri) with ContentResolver to get an InputStream (or perhaps a FileDescriptor) to pass to your library.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值