Krishna Sony
Krishna Sony

Krishna Sony

Google Play Policy For APP That Target Android 11 (API Level30)

Implement scope storage to use All File Access to access storage permission in Android 11

Krishna Sony's photo
Krishna Sony
·May 3, 2021·

3 min read

Google Play Policy For APP That Target Android 11 (API Level30)

👋Hello Android Folks! ,

In 2019, Google announced📢 a new feature called scope storage. Scoped Storage was introduced with Android 10 as a modern way for apps to access photos, videos, and other files on the device with privacy and user control in mind.

Scope Storage Introduced in Android 10

  • Unrestricted access to your own app storage
  • Unrestricted media and downloads contributions
  • Runtime permission only gives read access to media
  • User confirmation required for modifying media
  • Location metadata gated by new permission

💎For Android 10, you get benefits to opt-out to use scope storage using the requestLegacyExternalStorage flag. You just need to add android:requestLegacyExternalStorage="true" into your manifest file and you are done to skip using scope storage for Android 10.

But From Android 11 onward you are not able to opt-out to use scope storage.

It's mandatory for all apps that targeting Android 11 to use scope storage 😮.

Scope storage improvements for app targetting Android 11🎯

  • Enabled File Path APIs: It's basically an alternative to media store APIs
  • Bulk Media modification APIs: It's added the ability to make changes in bulk
  • All File Access: It list all permission in one place where the user can give access.
  • Private App Storage: Your app’s external storage directory won’t be accessible to 3rd party apps and vice versa

In this post now we only talk about how to access Read/Write storage permission for Android 11?. From 5th of May, We are eligible to use All File Access. So, Let's implement it 😀

First, Add the permission called MANAGE_EXTERNAL_STORAGE in your manifest file

<uses-permission
            android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
            tools:ignore="ScopedStorage" />

🔨 Let's create a function to check whether permission is given or not

fun checkPermission(): Boolean {
        return Environment.isExternalStorageManager()// it's available from Android 11
}

🎯 Check the device is targeting android 11 and above or not

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                if (checkPermission()){
                    // Here you can write code to access storage
                } else {
                   // request for permission to access storage
                }
   } else {
           // implement your older way to access storage
   }

🔓📂Now, let's create an extension to request permission to get access of storage

fun AppCompatActivity.openSettingsAllFilesAccess(requestCode: Int) {
    try {
        val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
        intent.addCategory("android.intent.category.DEFAULT")
        intent.data =
            Uri.parse(java.lang.String.format("package:%s", applicationContext.packageName))
        this.startActivityForResult(intent, requestCode)
    } catch (e: Exception) {
        val intent = Intent()
        intent.action = ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION
        this.startActivityForResult(intent, requestCode)
    }
}
//You can use FragmentActivity instead of AppCompatActivity if you are using in fragment

💻Our final code looks like this

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                if (checkPermission()){
                    // Here you can write code to access storage
                } else openSettingsAllFilesAccess(201)
   } else {
           // implement your older way to access storage
   }

📂↩️Handle the permission callback on onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 201) {
        if (SDK_INT >= Build.VERSION_CODES.R) {
            if (checkPermission()) {
                // perform action when allow permission success
            } else {
                Toast.makeText(this, "Allow permission for storage access!", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

Yeah! 🤓we are done😊 . Now we can access storage.

📝Note:- If you are using All File Access then you have to mention it on google play policy that what permission you are using and what for.

I hope you liked this article. If you find this article helpful then share it with everyone. Maybe it’ll help someone who needs it 😀

Thank You

 
Share this