BitmapFactory:无法解码流:java.io.FileNotFoundException:open faile



我授予了权限,但BitmapFactory.decodeFile()返回此错误:

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/DCIM/Camera/IMG_20200130_165131.jpg: open failed: EACCES (Permission denied)

我想从图库中获取最后一张图像并将其保存到另一个文件中。我在运行它的Android P测试设备上对其进行了测试。但它在安卓版本为 Q(API 级别 29(的安卓模拟器中返回此错误。这是怎么回事?

这些是我的代码片段:

writeStoragePermissionGranted();
BitmapFactory.Options options = null;
Bitmap bm = null;
String[] projection = new String[]{
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA,
MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
MediaStore.Images.ImageColumns.DATE_TAKEN,
MediaStore.Images.ImageColumns.MIME_TYPE,
MediaStore.Images.ImageColumns.DISPLAY_NAME,
};
final Cursor cursor = this.getContentResolver()
.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null,
null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
if (cursor.moveToFirst()) {
final ImageView imageView = (ImageView) findViewById(R.id.pictureView);
String imageLocation = cursor.getString(1);
File imageFile = new File(imageLocation);
if (imageFile.exists()) {
options = new BitmapFactory.Options();
options.inSampleSize = 2;
try {
bm = BitmapFactory.decodeFile(imageLocation, options);
} catch(Exception e) {
e.printStackTrace();
}
}
imageView.setImageBitmap(bm);

try {
saveImage(bm,"NAME" );
} catch (IOException e) {
e.printStackTrace();
}
}

writeStoragePermissionGranted()功能:

public void writeStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
startPeriodicRequest();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, Constants.REQUEST_CODE_WRITE_EXTERNAL_STORAGE_PERMISSION);
}
} else {
return;
}
} 

和清单权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

我知道对此有很多问题。但是没有人在安卓模拟器版本Q上工作。另一方面,此代码适用于安卓派(API 级别 28(

android:requestLegacyExternalStorage="true"添加到清单的应用程序块实际上可以解决问题!

在 Android 10 上,他们引入了"作用域存储"的概念,这样,您就无法再使用其路径打开图像。您可以在此处获取更多信息。

所以现在你必须 使用它的ParcelFileDescriptorUri对其进行解码 .

您可以:

final Cursor cursor = this.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, null, null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
if (cursor.moveToFirst()) {
final ImageView imageView = (ImageView) findViewById(R.id.pictureView);

if (Build.VERSION.SDK_INT >= 29) {
// You can replace '0' by 'cursor.getColumnIndex(MediaStore.Images.ImageColumns._ID)'
// Note that now, you read the column '_ID' and not the column 'DATA'
Uri imageUri= ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cursor.getInt(0));
// now that you have the media URI, you can decode it to a bitmap
try (ParcelFileDescriptor pfd = this.getContentResolver().openFileDescriptor(mediaUri, "r")) {
if (pfd != null) {
bitmap = BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor());
}
} catch (IOException ex) {
}
} else {
// Repeat the code you already are using
}
}

您只需要在Manifest.xml中将android:requestLegacyExternalStorage="true"添加到Application标签中即可。

我希望以下代码对您有所帮助。

清单.xml

在清单文件中添加以下内容:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

另添加:

在清单的应用程序标记中添加此项。

android:requestLegacyExternalStorage="true"

打开图库以获取图像:

private void openImagePicker() {
MyDatePicker datePicker = new MyDatePicker();
datePicker.show(getSupportFragmentManager(), "DATE PICK");
}

启动活动结果:

onActivityResult您将通过以下方式获得结果对象,您将获得图像原始 URI。

ActivityResultLauncher<Intent> galleryResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
imageURi = data.getData();
image.setVisibility(View.VISIBLE);
String path = getPath(imageURi);
File file = new File(path);
if (file.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
image.setImageBitmap(myBitmap);
}
}
}
});

获取图像的绝对路径:

public String getPath(Uri uri) {
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else return null;
}

最新更新