Capture Image from Camera and Display in Activity
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
Capturing an image from the camera and displaying it in an Android Activity involves launching the system camera app via an Intent, receiving the result, and loading the image into an ImageView. The modern approach uses the Activity Result API (registerForActivityResult) instead of the deprecated onActivityResult. For full-resolution images, you must provide a file URI via FileProvider. The thumbnail returned in the Intent extras is low resolution (around 160x120 pixels) and only suitable for previews.
Permissions
FileProvider is required on Android 7+ (API 24) because direct file URIs (file://) throw FileUriExposedException. The provider shares files securely via content:// URIs.
Thumbnail Only (Quick Approach)
TakePicturePreview() returns a small Bitmap thumbnail. No file URI or storage permissions are needed. Use this only when image quality does not matter.
Full-Resolution Image (Recommended)
TakePicture() saves the full-resolution photo to the URI you provide. The camera app writes directly to this file, and you load it afterward.
Java Version
Loading with Proper Orientation
Camera photos often have EXIF rotation metadata instead of actual pixel rotation. Without reading the EXIF data, images may appear rotated 90 degrees in the ImageView.
Using Glide for Efficient Loading
Glide (or Coil/Picasso) handles image loading, EXIF rotation, memory caching, and downsampling automatically. For production apps, always use an image loading library instead of setImageURI().
Common Pitfalls
- Using the thumbnail bitmap for anything beyond previews: The
data.getExtras().get("data")bitmap from the old Intent API is approximately 160x120 pixels. Always useTakePicture()with a file URI for usable-quality photos. - FileUriExposedException on Android 7+: Passing a
file://URI to the camera Intent crashes on API 24+. Always useFileProvider.getUriForFile()to create acontent://URI. - Image appears rotated: Many cameras save photos in landscape orientation with EXIF rotation metadata. Use
ExifInterfaceto read the orientation and rotate the bitmap, or use Glide which handles this automatically. - OutOfMemoryError with large images: Loading a full-resolution camera photo (12MP+) directly into a Bitmap consumes 48MB+ of memory. Use
BitmapFactory.Options.inSampleSizeto downsample, or use Glide which manages memory automatically. - Deprecated onActivityResult: The old
startActivityForResult/onActivityResultpattern is deprecated. UseregisterForActivityResultwithActivityResultContracts.TakePicture()for the modern, lifecycle-safe approach.
Summary
- Use
ActivityResultContracts.TakePicture()with aFileProviderURI for full-resolution photos - Use
TakePicturePreview()only for quick thumbnails that do not need to be saved - Configure
FileProviderin the manifest to avoidFileUriExposedExceptionon Android 7+ - Handle EXIF rotation with
ExifInterfaceor use Glide for automatic orientation correction - Use image loading libraries (Glide, Coil) in production to handle memory, caching, and rotation
- The Activity Result API replaces the deprecated
onActivityResultpattern
Related reading
- Cardinal direction algorithm in Java
- Case insensitive string as HashMap key
- Cassandra 3.0 and later require Java 8u40 or later
- Cassandra Java Driver- QueryBuilder API vs PreparedStatements
- Capture image via captureStillImageAsynchronouslyFromConnection with no shutter sound
- Capture iOS Simulator video for App Preview
- Cassandra Java Driver How are insert, update, and delete results reported?
- CassandraThe stack size specified is too small, Specify at least 228k
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.