Jakob
Jakob

Reputation: 1895

How to check if "Allow management of all files" is allowed?

There are basically 3 types of storage permissions in the android app settings:

enter image description here

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:

Thank you.

Upvotes: 1

Views: 1500

Answers (2)

Masoud Sobhi
Masoud Sobhi

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

blackapps
blackapps

Reputation: 9292

Environment.isExternalStorageManager().

Upvotes: 4

Related Questions