gpt4 book ai didi

android - 权限 READ_EXTERNAL_STORAGE 总是被拒绝

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:21:08 28 4
gpt4 key购买 nike

我正在尝试使用 Android 6.0 请求运行时权限。这是我的代码:

private void enablePermission() {
String[] PERMISSIONS = { Manifest.permission.READ_EXTERNAL_STORAGE};

if (!hasPermissions(this, PERMISSIONS)) {
ActivityCompat.requestPermissions(this, PERMISSIONS,
PERMISSION_ALL);
}
}


public static boolean hasPermissions(Context context, String... permissions) {

for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(context, permission) !=
PackageManager.PERMISSION_GRANTED) {
return false;
}
}

return true;
}


@Override
public void onRequestPermissionsResult(int requestCode, String[]
permissions, int[] grantResults) {
switch (requestCode) {
case PERMISSION_ALL:
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
Log.d("Permission", "Granted");
} else {
Log.d("Permission", "Denied");
}
}
}

它总是在 onRequestPermissionsResult 中告诉我“Permission Denied”。我用 ACCESS_FINE_LOCATION 尝试了这段代码,它成功了。

最佳答案

尝试根据您的权限添加 android 包。像这样(这是为了写外部存储):

public  boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(android.Manifest.permission. READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted");
return true;
} else {

Log.v(TAG,"Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission. READ_EXTERNAL_STORAGE}, 1);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG,"Permission is granted");
return true;
}


}

对于回调:

 @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
//resume tasks needing this permission
}
}

可以引用这个SO answer了解更多信息。

关于android - 权限 READ_EXTERNAL_STORAGE 总是被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36347034/

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