android pick images from gallery
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Picking images from the Android gallery has evolved significantly. The modern approach uses the Activity Result API with ActivityResultContracts.PickVisualMedia (Android 13+ Photo Picker) or GetContent for broader compatibility. The legacy startActivityForResult() with ACTION_PICK is deprecated since API 30. The Photo Picker provides a privacy-friendly experience without requiring storage permissions, while GetContent works on older Android versions. This article covers both modern and legacy approaches.
Modern Approach: Photo Picker (Android 13+)
The Photo Picker does not require any storage permissions. It runs in a separate process and only grants access to the specific files the user selects.
Using GetContent (Broader Compatibility)
GetContent uses ACTION_GET_CONTENT internally and works on Android 4.4+. It opens the system file picker or gallery app.
Processing the Selected Image
Jetpack Compose
In Jetpack Compose, use rememberLauncherForActivityResult() to register the picker. The AsyncImage composable (from Coil) handles loading and displaying the image.
Legacy Approach (Deprecated)
startActivityForResult() and onActivityResult() are deprecated. Use the Activity Result API (registerForActivityResult) instead.
Common Pitfalls
- URI permissions expire: URIs from the picker are temporary. If you need persistent access, copy the file to app storage or use
takePersistableUriPermission()withACTION_OPEN_DOCUMENT. Do not store the URI for later use without persisting permissions. - Not handling null URIs: The user can press back without selecting an image. Always check for
nullin the result callback before processing. - Using deprecated
startActivityForResult: This API is deprecated and will eventually be removed. Migrate toregisterForActivityResult()withActivityResultContracts. - Bitmap memory issues: Loading full-resolution images directly into memory causes
OutOfMemoryError. UseBitmapFactory.OptionswithinSampleSizeto downsample, or use libraries like Glide or Coil for efficient image loading. - Missing Photo Picker backport:
PickVisualMediarequires Google Play Services on devices running Android 11-12. Devices without Play Services fall back toACTION_GET_CONTENT. Test on both configurations.
Summary
- Use
ActivityResultContracts.PickVisualMediafor Android 13+ Photo Picker (no permissions needed) - Use
ActivityResultContracts.GetContentfor broader compatibility (Android 4.4+) - Copy selected files to app storage for persistent access — picker URIs are temporary
- Use
GetMultipleContentsorPickMultipleVisualMediafor multi-image selection - In Jetpack Compose, use
rememberLauncherForActivityResult()to register pickers - Avoid the deprecated
startActivityForResult/onActivityResultpattern
Related reading
- Android Preventing Double Click On A Button
- Android Push Notifications Icon not displaying in notification, white square shown instead
- Android RecyclerView addition removal of items
- Android Recyclerview GridLayoutManager column spacing
- Android Recyclerview vs ListView with Viewholder
- Android remove left margin from actionbar's custom layout
- Android Replace ... with ellipsis character
- Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead. Please use Android Tools > Fix Project Properties
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.