Take A Photo in Android Phone- App Dev Series 6

Objective: using Android device camera to take a photo in app.

As a modern app in many usage, having photo taking function is quite reasonable.
Today’s article we will create a function to our app that make the device(android) is able to take a photo in App via its camera.

Import plugin

This function requires a plugin developed by yasirkula, which can be downloaded from Assets Store of Unity or Github.
Once downloaded it, drag the Plugin folder into Assets of your project.

According to the update log in Github, you can use it directly after imported.

With plugin imported(installed), we can use the code example from Github.

This method is exactly showing how to use this taking photo function. We can copy this part of code and paste it to our script.
If there is no error appear on your code, then you are successfully imported the plugin without any error.

Press a button to take photo

In my app as example, I would like to enable the camera once I press the taking photo icon on my app. Then showing the photo on an raw image UI element after shot.

First we need to get the reference of raw image in script, which would be replaced the texture with taken photo.

Then paste in the code we copied from Github.

Inside this method, we need to remove the part that created a quad to storage the photo. Instead, we need to make the taken photo as a texture to replace the origin one from raw image.

Then create a public method to be called from a button click, to enable the camera function.

Don’t for get to set a maximum photo size as parameter.

Then assign this method to the button, and everything would be done!
You can build this app to check the result!



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
S.J. Jason Liu

S.J. Jason Liu


A passionate gamer whose goal is to work in video game development.