Everything about ActivityResult API

Milan Vadhel
2 min readMar 27, 2021

As an Android developer, we all know that we need to manage the runtime permissions while picking any image, video from the camera, or storage. We need to write more boilerplate code to manage request code for each and every permissions and that’s too annoying sometimes. There are also some third-party libraries are available to manage that all things like Dexter, PermisionsDispatcher, etc. But sometimes we expect something to handle with native codes rather than to use third-party libraries.

So that’s why I have thought to write the blog and share my knowledge with the community which helps you to handle runtime permissions, pick any content from storage with the new ActivityResult API from AndroidX.

What is ActivityResultAPI?

ActivityResult API provides some built-in contracts which commonly use while developing an android app such as :

StartActivityForResult()
RequestMultiplePermissions()
RequestPermission()
TakePicturePreview()
TakePicture()
TakeVideo()
PickContact()
CreateDocument()
OpenDocumentTree()
OpenMultipleDocuments()
OpenDocument()
GetMultipleContents()
GetContent()

Benefits:
- No need to manage to get the result from Fragment or Activity using different request codes.
- No need to write boilerplate code to handle the runtime permission

How to use it?
Here I will show you how to use it step by step.

Handle Runtime permissions:

Step 1:

Add below two dependencies in your app-level build.gradle file.

Step 2:

Register your ActivityResultLauncher to handle multiple permissions.

Step 3:

Request and Launch requestMultiplePermission Luncher.

How to Take Picture From Camera?

Step 1: Register your CaptureImage launcher to Take Picture from the camera

Step 2: Launch that launcher and pass Uri where you want to save your captured file

How to take video from Camera?

Step 1: Register your CaptureImage launcher to Take Picture from the camera

Step 2: Launch that launcher and pass Uri where you want to save your captured file

How to get Image, Video, or any content based on your MIME type?

Step 1: Register your Content Luncher

Step 2: Lunch it to get content

The full Source code is here :

--

--