The upcoming secure launch of Android 14 is quick approaching. Now is a superb time to check your app with this new launch’s modifications when you haven’t performed so already. With Platform Stability, you possibly can even submit apps focusing on SDK 34 to the Google Play Retailer.
Android 14 introduces a brand new function referred to as Chosen Images Entry, permitting customers to grant apps entry to particular photos and movies of their library, reasonably than granting entry to all media of a given kind. It is a wonderful means for customers to really feel extra comfy sharing media with apps, and it is also a good way for builders to construct apps that respect person privateness.
To ease the migration for apps that presently use storage permissions, apps will run in a compatibility mode. On this mode, if a person chooses “Choose pictures and movies” the permission will seem like granted, however the app will solely have the ability to entry the chosen pictures. The permission shall be revoked when your app course of is killed or within the background for a sure time (much like one time permissions). When the permission is as soon as once more requested by your app, customers can choose a special set of footage or movies if they need. As a substitute of letting the system handle this re-selection, it’s beneficial for apps to deal with this course of to have a greater person expertise.
Even when your app appropriately manages media re-selection, we imagine that for the overwhelming majority of apps, the permissionless photograph picker that we launched final yr would be the finest media choice resolution for each person expertise and privateness. Most apps permit customers to decide on media to do duties reminiscent of attaching to an e-mail, altering a profile image, sharing with mates, and the Android photograph picker’s acquainted UI offers customers a constant, high-quality expertise that helps customers grant entry in confidence, permitting you to deal with the differentiating options of your app. In case you completely want a extra tightly built-in resolution, integrating with MediaStore might be thought-about as an alternative choice to the photograph picker.
To make use of the photograph picker in your app, you solely must register an exercise end result:
|
The photograph picker permits customization of media kind choice between pictures, movies, or a particular mime kind when launched:
|
You may set a most restrict when permitting a number of picks:
|
Lastly, you possibly can allow the photograph picker assist on older units from Android KitKat onwards (API 19+) utilizing Google Play providers, by including this entry to your AndroidManifest.xml file:
|
In lower than 20 traces of code you’ve gotten a well-integrated photograph/video picker inside your app that doesn’t require any permissions!
Creating your individual gallery picker
Creating your individual gallery picker requires in depth improvement and upkeep, and the app must request storage permissions to get specific person consent, which customers can deny, or, as of Android 14, restrict entry to chose media.
First, request the proper storage permissions within the Android manifest relying on the OS model:
|
Then, the app must request the proper runtime permissions, additionally relying on the OS model:
val requestPermissions = registerForActivityResult(RequestMultiplePermissions()) { outcomes ->
|
With the Chosen Images Entry function in Android 14, your app ought to undertake the brand new READ_MEDIA_VISUAL_USER_SELECTED permission to manage media re-selection, and replace your app’s UX to let customers grant your app entry to a special set of photos and movies.
When opening the choice dialog, pictures and/or movies shall be proven relying on the permissions requested: when you’re requesting the READ_MEDIA_VIDEO permission with out the READ_MEDIA_IMAGES permission, solely movies would seem within the UI for customers to pick information.
|
You may test in case your app has full, partial or denied entry to the machine’s photograph library and replace your UX accordingly. It is much more vital now to request these permissions when the app wants storage entry, as an alternative of at startup. Understand that the permission grant might be modified between the onStart and onResume lifecycle callbacks, because the person can change the entry within the settings with out closing your app.
if ( |
When you verified you’ve gotten entry to the fitting storage permissions, you possibly can work together with MediaStore to question the machine library (whether or not the granted entry is partial or full):
information class Media( |
The code snippet above is simplified as an instance the right way to work together with MediaStore. In a correct manufacturing app, it is best to think about using pagination with one thing just like the Paging library to make sure good efficiency.
Chances are you’ll not want permissions
As of Android 10 (API 29), you not want storage permissions so as to add information to shared storage. This implies you can add photos to the gallery, file movies and save them to shared storage, or obtain PDF invoices with out having to request storage permissions. In case your app solely provides information to shared storage and doesn’t question photos or movies, it is best to cease requesting storage permissions and set a maxSdkVersion of API 28 in your AndroidManifest.xml:
|
ACTION_GET_CONTENT conduct change
In our final storage weblog publish, we introduced that we’ll be rolling out a conduct change every time ACTION_GET_CONTENT intent is launched with a picture and/or video mime kind. In case you haven’t examined but this alteration, you possibly can allow it manually in your machine:
adb shell device_config put storage_native_boot take_over_get_content true |
That covers the right way to provide visible media choice in your app with the privacy-preserving modifications we have made throughout a number of Android releases.If in case you have any suggestions or ideas, submit tickets to our challenge tracker.