BitmapFactory: Unable to decode stream: java.io.FileNotFoundException

試著忘記壹切 提交于 2019-12-03 08:58:53
Jd Prajapati

Its permission issue, you need to add permission in manifest for external read storage then after you can able to use it and if you are using os above 6.0 then you need use Easy permission.

For Write :

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

For Read:

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

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

You have ask run-time permission for READ-EXTERNAL storage when you start your CAMERA intent:

final int MyVersion = Build.VERSION.SDK_INT;
        if (MyVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
            if (!checkIfAlreadyhavePermission()) {
                ActivityCompat.requestPermissions(new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
            } else {
               startYourCameraIntent();
            }
        }

checkIfAlreadyhavePermission() method:

private boolean checkIfAlreadyhavePermission() {
        int result = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
        return result == PackageManager.PERMISSION_GRANTED;
    }

Handle Permission Dialog "Allow" and "Deny" button action in onRequestPermission():

@Override
        public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 1: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                   startYourCameraIntent();

                } else {
                    Toast.makeText(getActivity(), "Please give your permission.", Toast.LENGTH_LONG).show();
                }
                break;
            }
        }
    }

And add this to your Manifest's application tag:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!