How to use the Android Activity Result API for selecting and taking images

A short introduction to the ActivityResult API pre-built contracts

Yanneck Reiß
CodeX

--

Photo by Markus Spiske from Pexels

Retrieving an image by user input is a common task in Android development. Yet it is far from straightforward to implement it. In earlier stages of Android Development, letting the user take a picture and preview it took quite some effort. The same goes for selecting an image via the gallery app.

But as time goes by, Google provides more and more APIs that make these tasks, and thus our everyday life as developers, easier. Nowadays we have access to the ActivityResult API, which allows us to use ActivityResultContract API.

In earlier stages, if we wanted to get a result from an Intent, we had to override the onActivityResult()function, set request codes, and so on.

With the ActivityResult API, we are now able to facilitate this process, save ourselves a lot of code, and have a way more clear way of what data is actually received with the help of the ActivityResultContract .

Within this article, we want to focus on the following two contracts:

ActivityResultContracts.TakePicture()ActivityResultContracts.GetContent()

--

--

Yanneck Reiß
CodeX

Follow me on my journey as a professional mobile and fullstack developer