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

2023-12-19

我有一个关于 BitMapFactory.decodeFile 的问题。

在我的应用程序中,我希望用户能够从他/她的设备中选择图像或拍照。然后必须将其显示在 ImageView 中

这是代码片段:

Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String picturePath = cursor.getString(columnIndex);
                    cursor.close();
                    MyImage image = new MyImage();
                    image.setTitle("Test");
                    image.setDescription("test choose a photo from gallery and add it to " + "list view");
                    image.setDatetime(System.currentTimeMillis());
                    image.setPath(picturePath); 

我收到这个异常:

BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20170302-WA0012.jpg: open failed: EACCES (Permission denied)

如何解决。请帮助我。先谢谢了。


它的权限问题,您需要在清单中添加外部读取存储的权限,然后才能使用它,如果您使用的是6.0以上的操作系统,则需要使用Easy权限。

对于写:

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

供阅读:

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

6.0以上:

private String[] galleryPermissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};

if (EasyPermissions.hasPermissions(this, galleryPermissions)) {
            pickImageFromGallery();
        } else {
            EasyPermissions.requestPermissions(this, "Access for storage",
                    101, galleryPermissions);
        }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

BitmapFactory:无法解码流:java.io.FileNotFoundException 的相关文章

随机推荐