【问题标题】:Unable to decode stream: java.io.FileNotFoundException BitmapFactory无法解码流:java.io.FileNotFoundException BitmapFactory
【发布时间】:2018-08-28 01:38:30
【问题描述】:

我有一个定义权限的文件。该代码适用于较低版本的 android,但 8.0 及更高版本不适用。所以我明确要求进行自我许可检查。这段代码昨天还在工作,但突然我又遇到了同样的权限被拒绝错误。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (context.checkSelfPermission(Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED && context.checkSelfPermission(Manifest.permission.READ_PHONE_NUMBERS) != PackageManager.PERMISSION_GRANTED && context.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED && context.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED && context.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

                    //    Activity#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for Activity#requestPermissions for more details.
                    return null;
                }
            }

【问题讨论】:

  • 去设置,应用,检查你的应用的权限,因为你不是在请求权限,只是检查它
  • 文件是否存在于内部存储或 SD 卡中?
  • 你是对的。谢谢。如何在应用程序启动时启用它?我也在我的 androidmanifest.xml 中定义了它

标签: android android-permissions


【解决方案1】:

您需要请求权限,如果未授予权限,则显示占位符图像或错误。

来自https://developer.android.com/training/permissions/requesting

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
        Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {

    // Permission is not granted
    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) {
        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.
    } else {
        // No explanation needed; request the permission
        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
        // If the callback is successful, show image, otherwise error
    }
} else {
    // Permission has already been granted SHOW IMAGE
}

【讨论】:

    猜你喜欢
    • 2017-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 2019-11-11
    相关资源
    最近更新 更多