Search code examples
androidandroid-studioandroid-11

Image permission libraries for Android 11


As Google has changed the mechanism for the Image/Media Permission just like last time Google did this for lollipop. Now the old libraries that do this task very easy are no longer working for Android 11 devices.

What are the new Image/Media permission libraries for Android 11?


Solution

  • Write this permission in manifest

    <uses-permission
                android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
                android:minSdkVersion="30" />
    

    than add this code in your activity

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                if (!Environment.isExternalStorageManager()) {
                    requestPermission()
                } 
            }
    
    fun ManageStoragePermission() {
            alertDialog = alertdialog.builder(this@mainactivity).create()
            alertDialog.setMessage("You need to Allow Manage External files Permission")
            alertDialog.setButton(
                AlertDialog.BUTTON_POSITIVE,
                "OK"
            ) { dialog, which -> intentToPermisn() }
            alertDialog.setButton(
                AlertDialog.BUTTON_NEGATIVE,
                "Cancel"
            ) { dialog, which -> this.finish() }
            alertDialog.show()
        }
    
        private fun intentToPermisn() {
            val intent = Intent()
            intent.action = Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION
            startActivity(intent)
            manage_check = true
            alertDialog.dismiss()
        }