如何检查 Manifest.permission.MANAGE_EXTERNAL_STORAGE 是否被授予?
How to check if Manifest.permission.MANAGE_EXTERNAL_STORAGE is granted?
如何查看Manifest.permission.MANAGE_EXTERNAL_STORAGE
权限是否被授予?我尝试了以下但它不起作用:
ContextCompat.checkSelfPermission(context,
"Manifest.permission.MANAGE_EXTERNAL_STORAGE")
== PackageManager.PERMISSION_GRANTED
上面的代码总是returns false,尽管已经授予了权限。感谢任何帮助,谢谢!
if (ContextCompat.checkSelfPermission(
requireContext(),
Manifest.permission. MANAGE_EXTERNAL_STORAGE //Android 10, WRITE_EXTERNAL_STORAGE for older
) != PackageManager.PERMISSION_GRANTED
) {
requestPermissions(
arrayOf(Manifest.permission. MANAGE_EXTERNAL_STORAGE),
REQUEST_CODE. // you can use it then in onActivityResult() etc.
)
} else {
doSomething() //now you have permission
}
正如CommonsWare所说,您可以使用isExternalStorageManager()
Returns 调用应用程序是否具有主 shared/external 存储介质上的所有文件访问权限。
声明权限 Manifest.permission.MANAGE_EXTERNAL_STORAGE
不足以获得访问权限。
要请求访问权限,请使用 android.provider.Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION
.
例子
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if(!Environment.isExternalStorageManager())
{
try {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse(String.format("package:%s", getApplicationContext().getPackageName())));
storagePermissionRequest.launch(intent);
} catch (Exception e) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
storagePermissionRequest.launch(intent);
}
}
}
结果合同。 ActivityResultContracts
ActivityResultLauncher<Intent> storagePermissionRequest = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (!Environment.isExternalStorageManager()) {
showPermissionDeniedDialog();
}
}
}
});
希望对大家有所帮助
如何查看Manifest.permission.MANAGE_EXTERNAL_STORAGE
权限是否被授予?我尝试了以下但它不起作用:
ContextCompat.checkSelfPermission(context,
"Manifest.permission.MANAGE_EXTERNAL_STORAGE")
== PackageManager.PERMISSION_GRANTED
上面的代码总是returns false,尽管已经授予了权限。感谢任何帮助,谢谢!
if (ContextCompat.checkSelfPermission(
requireContext(),
Manifest.permission. MANAGE_EXTERNAL_STORAGE //Android 10, WRITE_EXTERNAL_STORAGE for older
) != PackageManager.PERMISSION_GRANTED
) {
requestPermissions(
arrayOf(Manifest.permission. MANAGE_EXTERNAL_STORAGE),
REQUEST_CODE. // you can use it then in onActivityResult() etc.
)
} else {
doSomething() //now you have permission
}
正如CommonsWare所说,您可以使用isExternalStorageManager()
Returns 调用应用程序是否具有主 shared/external 存储介质上的所有文件访问权限。
声明权限 Manifest.permission.MANAGE_EXTERNAL_STORAGE
不足以获得访问权限。
要请求访问权限,请使用 android.provider.Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION
.
例子
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if(!Environment.isExternalStorageManager())
{
try {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse(String.format("package:%s", getApplicationContext().getPackageName())));
storagePermissionRequest.launch(intent);
} catch (Exception e) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
storagePermissionRequest.launch(intent);
}
}
}
结果合同。 ActivityResultContracts
ActivityResultLauncher<Intent> storagePermissionRequest = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (!Environment.isExternalStorageManager()) {
showPermissionDeniedDialog();
}
}
}
});
希望对大家有所帮助