尝试从图片中读取元数据时获取FileNotFoundException(权限被拒绝) [英] Getting FileNotFoundException (Permission denied) when trying to read metadata from a picture
问题描述
我正在通过我的应用访问设备图库的图片,当访问图片时,图片的元数据将被读取并存储在metadata
中.我面临的问题是,每当程序尝试读取元数据时,都会出现以下错误java.io.FileNotFoundException: /storage/emulated/0/Snapchat/Snapchat-1185425082.jpg (Permission denied)
.
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);
}
}
推荐答案
从策略上看,您似乎没有READ_EXTERNAL_STORAGE
权限,该权限
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.
除此之外:
-
您的
query()
无需返回DATA
列
不要求DATA
列具有值
不要求DATA
列具有文件系统路径
There is no requirement that the DATA
column have a filesystem path
即使READ_EXTERNAL_STORAGE
特别是,可以确保您的代码在Android Q上失败,并且在许多其他设备上的许多用户也很可能失败.
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.
将Uri
(imageUri
)与ContentResolver
一起使用,以获取InputStream
(或者可能是FileDescriptor
)传递给您的库.
Use the Uri
(imageUri
) with ContentResolver
to get an InputStream
(or perhaps a FileDescriptor
) to pass to your library.
这篇关于尝试从图片中读取元数据时获取FileNotFoundException(权限被拒绝)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!