Flutter -Storage access for Android SDK 33
Published: 2023-03-20 21:37:21


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfet38d54zq2zdyz1hte.gif)


## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1xu67vunn6n0ffypuc7w.gif)

## 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 ](https://mukhtharcm.com/storage-permission-in-flutter/)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](https://developer.android.com/about/versions/13/behavior-changes-13?hl=pt-br#granular-media-permissions)**.

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 ](https://pub.dev/packages/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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bpmkiyzltkhsazr2n16h.gif)

## 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 ](https://pub.dev/packages/device_info_plus)library and made the changes, this was the final result:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s6gem7w46bvnc3rca951.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e29xfx0gw1c5wx0j3g6v.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sor8eh6w4q1fdh9dsn72.png)

> 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dxffk6knt7dkqisla9p2.gif)
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 ](https://pub.dev/packages/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](https://developer.android.com/reference/android/Manifest.permission), Link [two](https://developer.android.com/training/data-storage/shared/media?hl=pt-br#storage-permission), Link [three ](https://developer.android.com/about/versions/13?hl=pt-br)and Link [four](https://developer.android.com/about/versions/13/behavior-changes-13?hl=pt-br#granular-media-permissions).

Thank you for reading.

You can reach and follow me on **Marco4763**: [Github ](https://github.com/Marco4763)and [LinkedIn](https://www.linkedin.com/in/marco-domingos-239756171/).