Hack: What You Ought to Know About The Android Camera Intent
By: Hannah Olukoye
Tags: Imageview, Camera, Android, Intent, Bitmap

One of the most interesting projects I have worked on in Android in the past included implementing a working camera module on an app written in react native. I won't go into the details of the challenges we faced while trying to complete this task but I will share in this article something that I found a highlight out of the entire experience.

According to the documentation for developers on android, the basics of taking photos on an android app involve the use of an Intent (an abstract description of an operation to be performed) which consists of the following steps.

1) The Intent 2) A call to start the external Activity 3) The code to handle the image data.

Many times we follow a code snippet/solution on stack overflow expecting to find an exact implementation for what we want, only to run the application on a device and then realize it would have made more sense to read the documentation first.

If you have followed some/many of the tutorials on how to capture and display an image on android you will find that the image displayed is (most times) blurry.

I will walk you through a basic setup to capture a photo and display the same photo as-is on an image view in Android.

1. Create the main Activity (CaptureActivity.java)

This is where all the magic happens.

2. Create the XML Layout File (activity_capture.xml)

This will define the view item you would like to see.

<?xml version="1.0" encoding="utf-8"?>



<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">





<ImageView

android:id="@+id/imageView"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:adjustViewBounds="true"

android:scaleType="fitXY"

/>



</LinearLayout>

3. Add permissions on the Manifest File (AndroidManifest.xml)

This is important to allow the application to access the camera on the device and to store the image on the device memory.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.CAMERA" />

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />



<uses-feature

android:name="android.hardware.camera.any"

android:required="true" />

<uses-feature

android:name="android.hardware.camera.autofocus"

android:required="false" />

Now that we have all the important files setup. You can go ahead and run our application. The main point of all this is to remember not to use the onActivityResult’s data. As shown in this bit of code.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK && requestCode == 100) {

mImageView.setImageURI(imageUri);



}



}

By default, onActivityResult returns data as null and MediaStore returns a default path which is the last gallery image, not your exact photo. You can capture this by adding these lines to your code.

imageUri = data.getData();

if (imageUri == null) {

Toast.makeText(this, "Missing Image", Toast.LENGTH_SHORT).show();

}

Special thanks to a guru developer friend.

References.