⏹ StorageAccess Extension: Simplifying Storage Permissions in MIT App Inventor

Intoduction

Introducing the StorageAccess Extension create by (me)Mr_Koder

The StorageAccess extension is a valuable addition to MIT App Inventor, offering simplified storage permissions management and Android SDK version retrieval. With this extension, developers can effortlessly handle storage-related tasks and ensure compatibility across different devices. Let's explore how the StorageAccess extension simplifies storage access and enhances App Inventor projects.

Clarification
Starting from Android 11 (SDK level 30), Google introduced scoped storage which limits access to external storage by apps for better privacy. Here are the details:

  • Apps can still read and write files in their own specific directories in external storage.
  • For media files (Images, Videos, Audio), apps can use the MediaStore API to access shared storage.
  • For non-media files, apps need to use the Storage Access Framework (SAF) to interact with files. This allows users to select specific files or directories that your app can access. (solved by this extension)

Blocks

RequestStoragePermission
This method is used to request storage permissions from the user. If the Android SDK version is below 30, it requests the READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions using ActivityCompat.requestPermissions() . If the Android SDK version is 30 or above and the app does not have the necessary permission to manage external storage, it opens the system settings page for the user to grant the permission.





OnManageExternalStoragePermissionGranted
Event is triggered within the StorageAccess extension to notify the app when the user grants the manage external storage permission.




CheckStoragePermission
this method is used to check the storage permissions of the app. If the app has the necessary permission to manage external storage on Android devices with SDK version 30 or above, it triggers the OnManageExternalStoragePermissionGranted() event and reports that the permission has been checked and granted through the true event. Otherwise,
it triggers the false event to indicate that the permission is not granted or the device's SDK version is below 30.




OnPermissionChecked
event is triggered within the StorageAccess extension to notify the app when the permissions have been checked. It provides the app with information about whether the required storage permission is granted or not,





GetSdkVersion`
method is used to retrieve the Android SDK version of the device.




Video

AixFile

StorageAccess.aix (7.6 KB)
Thanks

3 Likes

I didn't see the video earlier.
If you are not able to access files even after using SAF Picker then you have done something incorrectly in your blocks.

Regarding SAF, here it is

This extesnion does not directly deal with the Storage Access Framework (SAF). Instead, it focuses on managing storage permissions and retrieving the grant access status.

I think you should be more specific about your words.

If you are using SAF then there is no need to ask for MANAGE_EXTERNAL_STORAGE permission, but in the preview video you have shown that you were unable to upload file without this permission. How is that even possible?
It all seems so confusing to me.

Google Play will also reject your app with this permission (most of the time).

in the video, I used a device with Android 11 (SDK30) so I had to request access to get non-media files (like PDF)

You can get access to all type of files with SAF. See preview video of this extension:

You are correct, I know this,
Cause Google’s policy requires that apps justify the need for this permission and demonstrate core functionality that requires such broad access to manage all files. Without a legitimate need for this permission, apps may be rejected from the Google Play Store.

in the video, I could upload images, videos, or even Audio files by default before requesting access

but if I decided to pick any other file types I had to request this permission .

Which extension did you use to pick files?

I use Activity starter

Use SAF and you will find that you can access all documents without any need of permission.

do you mean SAF Picker :point_down: ?

Yes.

Ok I will definitely give it a try thanks @vknow360

Yes, this will only confuse most users and everything has already been explained in detail in this guide:

4 Likes

Hello, I stumbled upon this post created by you nine months ago. Thanks to your extension, I managed to complete my app. However, I still have some issues, and I kindly ask if you could please send me the AIA file of the video so that I can better understand the functioning or the logic of your extension. Let me explain further, I am currently unable to find the logic "if the permissions are granted, then do". Best regards.

This block returns true when successfully granted and false for counter.

Hi Black_Knight, thanks for the answer, but i can t see the block that you mentioned.

I presume it is this one:

See first post