简体   繁体   English

java.lang.SecurityException:权限拒绝:阅读com.android.providers.media.MediaProvider

[英]java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider

I want to select image and save it in database ,when select image from gallery give me exception: 我想选择图像并将其保存在数据库中,当从图库中选择图像时出现异常:

在此处输入图片说明

the error in onSelectFromGalleryResult method: onSelectFromGalleryResult方法中的错误:

@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case Utility.MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    if(userChoosenTask.equals("Take Photo"))
                        cameraIntent();
                    else if(userChoosenTask.equals("Choose from Library"))
                        galleryIntent();
                } else {
            }
            break;
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == SELECT_FILE)
            onSelectFromGalleryResult(data);
        else if (requestCode == REQUEST_CAMERA)
            onCaptureImageResult(data);
    }
}
@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
    Bitmap bm=null;
    if (data != null) {


        try {
    ##        bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());##
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    ivImage.setImageBitmap(bm);
}`

manifest file 清单文件

在清单文件上方的应用程序标签上授予权限

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

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. 从Android 6.0(API级别23)开始,用户在应用程序运行时(而不是在安装应用程序时)授予应用程序权限。

Below describes methodology to check, and request, permissions. 下面介绍了检查和请求权限的方法。 However, using the support library is simpler, since your app doesn't need to check which version of Android it's running on before calling the methods. 但是,使用支持库更为简单,因为在调用方法之前,您的应用程序无需检查正在运行的Android版本。

For more info, check out this Official documentation 有关更多信息,请查看此官方文档

Declaring permission to be checked, 声明要检查的权限,

private static final int REQUEST_STORAGE_PERMISSIONS = 1;
private static final String[] STORAGE_PERMISSIONS = {
        Manifest.permission.READ_EXTERNAL_STORAGE,
};

Below function will check if the permission is granted, 下面的功能将检查是否授予了权限,

private boolean hasPermissionsGranted(String[] permissions) {
    for (String permission : permissions) {
        if (ActivityCompat.checkSelfPermission(getActivity(), permission)
                != PackageManager.PERMISSION_GRANTED) {
            return false;
        }
    }
    return true;
}

Use above function to target code which require critical permission to be granted, here is the code to do that - 使用上面的函数来定位需要授予关键权限的代码,这是执行此操作的代码-

if (!hasPermissionsGranted(STORAGE_PERMISSIONS)) {
        requestPermissions();
        return;
    }

Now, here is the actual code which triggers permission dialog to grant the permission from user, 现在,这是触发权限对话框以授予用户权限的实际代码,

private void requestPermissions() {
    if (shouldShowRequestPermissionRationale(STORAGE_PERMISSIONS)) {
        showPermissionDialog(); // Dialog to describe user how permission is important
    } else {
        requestPermissions(STORAGE_PERMISSIONS, REQUEST_STORAGE_PERMISSIONS);
    }
}

Now, Handle the permissions request response, 现在,处理权限请求响应,

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    if (requestCode == REQUEST_STORAGE_PERMISSIONS) {
        if (grantResults.length == STORAGE_PERMISSIONS.length) {
            boolean isGranted = true;
            for (int result : grantResults) {
                if (result != PackageManager.PERMISSION_GRANTED) {
                    isGranted = false; // You can tell user that critical permission denied
                    break;
                }
            }
            if (isGranted) { // perform that critical operation here... }
        } else {
            // Tell user that critical permission denied
        }
    } else {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.SecurityException:Permission Denial:在Android中读取com.android.providers.media.MediaProvider同时从图库中获取图片 - java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider in Android while taking picture from gallery 拒绝权限:阅读com.android.providers.media.MediaProvider uri content:// media / external / images / media / 38 - Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/38 应用程序崩溃引发权限拒绝异常:读取com.android.providers.media.MediaProvider - App Crashes throwing an exception of permission denial:reading com.android.providers.media.MediaProvider java.lang.SecurityException:权限被拒绝:打开提供程序 com.android.providers.media.Media - java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.Media java.lang.SecurityException:权限拒绝:打开提供程序 com.android.providers.calendar.CalendarProvider2 - java.lang.SecurityException: Permission Denial: opening provider com.android.providers.calendar.CalendarProvider2 java.lang.SecurityException:权限拒绝:阅读 - java.lang.SecurityException: Permission Denial: reading java.lang.SecurityException:Permission Denial:需要com.huawei.android.launcher.permission.WRITE_SETTINGS - java.lang.SecurityException: Permission Denial: requires com.huawei.android.launcher.permission.WRITE_SETTINGS java.lang.SecurityException:权限拒绝:打开提供者com.google.android.apps.photos.content.GooglePhotosImageProvider - java.lang.SecurityException: Permission Denial: opening provider com.google.android.apps.photos.content.GooglePhotosImageProvider java.lang.SecurityException:权限拒绝:启动Intent com.android.calendar / .DayActivity - java.lang.SecurityException: Permission Denial: starting Intent com.android.calendar/.DayActivity Android:java.lang.SecurityException:权限拒绝:启动意图 - Android: java.lang.SecurityException: Permission Denial: start Intent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM