gpt4 book ai didi

android-permissions - 由于拒绝权限,MediaScannerConnection在Android 6上失败

转载 作者:行者123 更新时间:2023-12-04 04:41:04 24 4
gpt4 key购买 nike

我使用MediaScannerConnection调用其scanFile方法,以将图像添加到设备库中。但是在Android 6中,执行它时会收到以下异常:

E/DatabaseUtils: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/fs_id from pid=22984, uid=10078 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()





E/iu.UploadsManager: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/fs_id from pid=22984, uid=10078 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()



有什么帮助吗?

最佳答案

这是使用新方法处理权限的结果。您的应用程序现在需要寻求运行时许可,并准备被拒绝。

在您的onCreate(或任何地方)中进行检查,并可能请求许可:

            if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {

// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_EXTERNAL_STORAGE)) {

// Show an expanation 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, we can request the permission.

ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_MEDIA);

// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}

然后准备接受这样的权限:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_MEDIA: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
}
}

关于android-permissions - 由于拒绝权限,MediaScannerConnection在Android 6上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33129884/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com