Android Capturing Images from Camera or Gallery as Bitmaps

Photo by Eaters Collective on Unsplash

Capturing images using a camera and using it in your Android applications can be tricky at times but the process is quite simple.

So in this story, I will teach you to get images from the gallery or capture images using a camera and then getting those images as bitmaps in our applications.

So firstly create a new Android studio project and select Java as the programming language. For Kotlin click here.

Layout

Now in the activity_main.xml file place an ImageView widget

Now inside MainActivity create and initialize this imageview.

Permissions

As we want to capture images from the camera so we need the camera permission and then to save those images we need storage permission so in the manifest file add these lines

Ask for these permissions dynamically inside MainActivity. So add these lines inside the onCreate method of MainActivity.

Capturing image using camera

Let’s say when the user will long press on the imageView we want to open the camera and capture the image. Then we want to display that captured image inside our Android application.

So to achieve that set OnLongClickListener on the imageView. So inside onCreate method paste the below code.

So inside this method, we are again checking if the camera and storage permission is granted or not. If the permission is not granted then we are asking for permission again otherwise we are calling a method name openCamera. Inside this method, we have the code to launch the camera. So place this code below onCreate method.

Now inside this method, we are launching an intent to open the camera, and once the user will capture the image then the URI of that image will be stored inside the image_uri variable. But to ensure that the user has captured the image we will add the onActivityResult method and check if image an image was captured or not. So add this method below openCamera method.

Converting Image into bitmap

Now we have the captured image URI and we are displaying that image inside the image view. But to get the image in a bitmap format place this uriToBitmap method inside MainActivity

Now call this method inside onActivityResult and pass the image uri to get that image in a bitmap format.

And that’s it. Now when you will run this application you will be able to capture an image from the camera and then that image will be displayed inside imageview. As we also converted that image into a bitmap so you can use it for a variety of different purposes.

Choosing Images from gallery

Let’s say we want to choose the image from the gallery when the user will click on the image view that we placed earlier inside our application layout.

So inside onCreate method set OnClick listener for the image view.

Now there you can see when the user will click on the imageview we are launching an intent to open the gallery. And once the user will select an image onActivityResult method will be called and we can get the URI of the selected image inside that method. So add this code inside onActivityResult method

So inside this method, we are getting chosen image Uri and displaying that image inside our image view.

Now when you will run this application you will be able to choose an image from the gallery as well.

Converting Image into bitmap

Now we have the Uri of the image and if you want to get the image as Bitmap So call the uriToBitmap method again. So the code inside onActivityResult will be

And that’s it, now you can run the application and test it. You can also get the application code from Github.

Train your own Image Recognition models and build real-time android applications with our “Image Recognition in Android One hour Bootcamp

To Learn Android Machine Learning checkout our Android Machine Learning with TensorFlow lite in Java/Kotlin

Udemy Instructor, Helping people Integrate Machine Learning in Android & IOS . Visit my courses https://www.udemy.com/user/e1c14fb5-1c9b-45ef-a479-bbc543e33254/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store