DEV Community

Cover image for Flutter -Storage access for Android SDK 33
Marco Domingos
Marco Domingos

Posted on

Flutter -Storage access for Android SDK 33

Recently Android SDK 33 became available for most devices and with that, came the task for many developers of upgrading the **compileSdkVersion **to the **TIRAMISU **version code. But, what many of these developers forgot to check was, what changes in this new version? Mostly Flutter developers.

Image description

Hum… Why Especially Flutter?

As Google itself defines, Flutter is an open source framework by Google for building beautiful, natively compiled, multi-platform applications from a single codebase.

Flutter is still growing and currently most of the Flutter features that developers use rely on third-party libraries built with native code. This means that before changing the compileSdkVersion, the developer must make sure that the third-party library has also made the necessary changes to work with their new sdk version.

Image description

Like What?

Recently in the Community, someone ask me to help him with a code that he was using to practice his knowledge of Flutter. So, he was following this article and overall there is nothing wrong with the article, the problem was that according to him the storage permission worked in some devices, but not in his device, later he told me that the device was with the last update of android 13 (Tiramisu) and with that the reason why it wasn't working was clear to me.

What most dev doesn't know is that since Android 10, the app doesn't need to ask the storage permission since the app by default has access to any files that the device owns, this means files that weren't generated by others apps and are stored in unusual storage. But these is only visible now because only in android 13 that they start using Granular Media Permissions, and with that the app no longer ask for the storage permission in the app as before, in some devices you can still set it manually at the app info page on the device configurations. You can read more about it here.

So, basically what he was doing wrong is checking if the storage was granted in app running on device with Tiramisu, by default the permission will be PermanentlyDenied and even if the user opens the app info to allow it manually, is still a long shot, as I said before, only in some devices the storage permission will appear in the app info, not all of them.

Before I gave him the solution, I decided to check the library he was using to confirm that they where working with Granular Media, that was the permission_handler library, and even the lib being updated, I was surprised to find out that many others devs were having the same difficult, they were asking and checking for the storage permission on devices running the SDK 33.

Image description

What was the solution?

Well, there were three possible solutions:

1 - Solution One
He could simply remove the check option in general, but this was not scalable as the app run on older versions of android.

2 - Solution Two
I simply suggested him to before ask and check the storage permission, he needed to check if the SDK version that is running is older that Tiramisu, so he add the device_info_plus library and made the changes, this was the final result:
Image description

3 - Solution Three
If there was the need to access some of the files generated by others apps, then he should use Granular Media Permissions, this knowing that the library was already working with them.
So first he just had to add one of these permissions, with Granular Media Permissions he just had to specify the type of media he needed access to:

Image description
And then use the permission that the lib has prepared for this type of situation, which in this case can be Permission.photos, Permission.videos or Permission.audio, like this:

Image description

Note: if you need the access for both photos and videos, you can use either Permission.photos or Permission.video, you don't need both of them, because in Granular Media the access is granted for both media types.

Image description
And with that the problem was solved.

Conclusion

So in the end we only needed to add a few extra lines of code and the code worked perfectly.

With this type of update it is always good to check the library before making the app available for any recent SDK, not every library is constantly updated. In this case, permission_handler have made the changes and is working with Granular Media Permissions, but not all stories have a happy ending.

You can read more about Granular Media Permissions and Tiramisu on this links: Link one, Link two, Link three and Link four.

Thank you for reading.

You can reach and follow me on Marco4763: Github and LinkedIn.

Top comments (0)