Reputation: 1895
There are basically 3 types of storage permissions in the android app settings:
My app requires the highest permission, so I use <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
in manifest.xml
.
Now I want to block the whole app, if the permission is not granted. So I tried using this code to check, if the highest permission is granted, but it always returns false.
if(ActivityCompat.checkSelfPermission(requireActivity(), Manifest.permission.MANAGE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
I also tried using WRITE_EXTERNAL_STORAGE
, but it also returns true at the second option "Allow access to media only".
Conclusion
I am looking for a boolean
function:
Returning true, if "Allow management of all files" is selected
Returning false, if "Allow access to media only" is selected
Returning false, if "Deny" is selected
Thank you.
Upvotes: 1
Views: 1500
Reputation: 1
this code work for me
fun hasAllFilesAccess(context: Context): Boolean {
val appOpsManager = context.getSystemService(APP_OPS_SERVICE) as AppOpsManager
try {
val app = context.packageManager.getApplicationInfo(context.packageName, 0)
return appOpsManager.unsafeCheckOpNoThrow(
"android:manage_external_storage",
app.uid,
context.packageName
) == AppOpsManager.MODE_ALLOWED
} catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace()
} catch (e: NoSuchMethodError) {
return true
}
return false
}
Upvotes: 0