How to Build a Photo Viewing App in Android
In this article
You can build a photo viewing application in Android by creating a project in Android Studio, adding a grid of image thumbnails using RecyclerView, and showing the full-size image when a thumbnail is tapped. The simplest working version needs no special permissions if you use the system photo picker to let users choose images, and it can be built with standard AndroidX libraries. This guide walks through the practical steps, from project setup to handling large images without crashing.
What You'll Need
- Android Studio installed on your computer (the official IDE from Google).
- A basic understanding of Kotlin or Java, and XML layouts.
- An Android phone or emulator running a recent Android version for testing.
- An internet connection for downloading dependencies the first time you build.
Step-by-Step
- Create a new project. Open Android Studio and choose New Project. Select the Empty Views Activity template (or Empty Activity if you prefer Compose). Name it something like PhotoViewer, choose Kotlin, and set the minimum SDK to API 21 or higher. Click Finish and let Gradle sync.
- Add dependencies. In your app-level build.gradle file, add the RecyclerView and Glide libraries. RecyclerView is part of AndroidX; Glide handles image loading and caching. Use the latest stable versions shown in Android Studio's suggestions. Sync the project after adding them.
- Design the main layout. Open activity_main.xml. Add a RecyclerView that fills the screen. Give it an ID such as photoGrid. Optionally add a TextView that shows when the list is empty.
- Create an item layout for each photo. Make a new XML file called item_photo.xml. Use an ImageView with a fixed width and height (for example 120dp by 120dp) and a scaleType of centerCrop so thumbnails look neat. Add a small margin.
- Build the adapter. Create a Kotlin class PhotoAdapter that extends RecyclerView.Adapter. It should hold a list of image URIs. In onCreateViewHolder, inflate item_photo.xml. In onBindViewHolder, load the image into the ImageView using Glide.with(context).load(uri).into(imageView). Set a click listener on the itemView to open a detail screen.
- Let the user pick photos. On Android 13 and later, use the Photo Picker via ActivityResultContracts.PickMultipleVisualMedia. This requires no storage permission. On older versions, the same contract falls back to a system picker. Add a button in the layout labelled "Add Photos" and launch the picker from it. When the result returns, add the URIs to your adapter's list and call notifyDataSetChanged.
- Create the detail screen. Add a second activity called PhotoDetailActivity. Its layout contains a single ImageView with scaleType fitCenter. Pass the selected URI as a string extra in the Intent. In the activity, retrieve the extra, parse it to a Uri, and load it with Glide into the ImageView. Add a back button or rely on the system back gesture.
- Handle large images safely. Full-resolution photos from modern phones can be very large. Glide downsamples automatically when you give it a target size, but for the detail view you can request a larger size with override(). Avoid loading the original bitmap directly with BitmapFactory without sampling, or you risk OutOfMemoryError.
- Test on a device. Run the app on an emulator or a physical phone. Add several photos, scroll the grid, tap one, and rotate the screen to check that state survives. If the grid resets on rotation, save the list of URIs in onSaveInstanceState.
Common Mistakes
- Requesting READ_EXTERNAL_STORAGE on Android 13+ when the Photo Picker needs no permission. This causes unnecessary permission prompts and can confuse users.
- Loading full-size images directly into small ImageViews without downsampling, leading to memory crashes.
- Forgetting to set a click listener inside onBindViewHolder, so tapping a thumbnail does nothing.
- Using notifyDataSetChanged for every small change; prefer notifyItemInserted or DiffUtil for smoother updates.
- Not handling the case where the user cancels the picker, which can leave the app in an inconsistent state if you assume a result always arrives.
Tips
- Use Glide's placeholder and error drawables so the grid looks polished while images load or if a file is missing.
- Cache the list of URIs in SharedPreferences if you want the selection to persist between app launches.
- For a modern approach, consider Jetpack Compose with the Coil library, which reduces boilerplate for image loading.
- Test with a mix of small and very large images, including HEIC files if your target devices support them.
- Keep your minimum SDK reasonable; API 21 covers most active devices and avoids legacy workarounds.
Troubleshooting
If the app crashes when opening a photo, check Logcat for OutOfMemoryError. Reduce the requested image size in Glide with override(1024, 1024) or use a smaller target. If thumbnails appear blank, confirm the URI is valid and that you have not revoked access by moving or deleting the file. If the Photo Picker does not appear on an older device, the fallback may open a different system UI; this is expected. If the grid does not update after adding photos, ensure you call the adapter's notify method on the main thread. If the app fails to build after adding dependencies, sync Gradle and check for version conflicts in the build file. For any permission-related issue, review the manifest and remove unnecessary permissions. If the detail screen shows a black image, verify the extra key matches exactly between the two activities.
Frequently Asked Questions
Do I need storage permissions to view photos?
Not if you use the system Photo Picker. It grants temporary access to the selected images without broad storage permissions. Only request permissions if you need to scan the entire gallery automatically.
Can I build this without Android Studio?
You can use command-line tools and Gradle, but Android Studio provides the emulator, layout editor, and Logcat, which make development much faster. It is the recommended environment.
How do I show a slideshow?
Use a ViewPager2 with an adapter that loads each URI in order. Add a timer or a next button to advance pages. Glide handles the image loading per page.
Why does my app use so much memory?
Large bitmaps are the usual cause. Always downsample images to the display size. Glide does this by default when you provide a target size, but check that you are not overriding with original size.
Can I view photos from a specific folder only?
Yes, but that requires querying MediaStore with a folder filter, which needs the READ_MEDIA_IMAGES permission on Android 13+. The Photo Picker does not support folder filtering; it shows all images the user selects.
Is it possible to edit photos in the app?
Basic edits like crop or rotate can be done with Android's built-in image editing intents or libraries like Android Image Cropper. Full editing is beyond a simple viewer and adds significant complexity.