Store Captured Image in SD card using Android Camera Application

Store Captured Image in SD card using Android Camera Application

In this tutorial we are going to learn how to store captured image in SD card using Android built in camera application. This will be quite similar to my last tutorial on Display Captured Photo in ImageView using Android Camera Application.

The only difference between these two android tutorials is that we will make use of the SD card for storing our captured image while in the former, we captured image from our camera and display it using ImageView control.

If you don’t want to use the in-built camera app in your device rather you prefer to use the android camera API, I will suggest you read my tutorial on Android Camera API

I will only talk about what is difference from the last tutorial here. Make sure the read the tutorial before reading this one. Although everything is layout in a manner that is very simple to understand.

Before we start, the first thing I will do is to list the environment and tools I used in this android tutorial but feel free to use whatever environment or tools you are familiar with.

Windows 7

Android Studio

Samsung Galaxy Fame Lite

Min SDK 14

Target SDK 19

To create a new android application project, following the steps as stipulated below.

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: AndroidCameraSDCard

Package: com.inducesmile.androidcamerasdcard

Keep other default selections.

Continue to click on next button until Finish button is active, then click on Finish Button

Once you are done with creating your project, make sure you change the package name if you did not use the same package.

Manifest.xml File

Since we are going to access and make use of SD card in our device, we are going to use some android permissions for write and read to the SD card. The updated version of our Manifest.xml file is shown below.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    package="" >
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        android:theme="@style/AppTheme" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />


 Activity_main.xml Layout File

Open the activity_main.xml in your project or the layout name you want to use. We are going to add two different View controls in our layout file as shown below.

<RelativeLayout xmlns:android=""

        android:layout_alignParentStart="true" />

        android:layout_centerHorizontal="true" />


 String.xml File

We have add some string resource in our project string.xml file. The updated version of the file looks as shown below. The added resources are used for the button name and the image description.

    <string name="app_name">Android Camera SD Card</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="capture_button">Click to capture photo</string>
    <string name="desc">Capture image holder</string>
</resources> File

Open the file in your project. We are going to create an instance of each of our View Controls

We have initialized a class attribute which will use to result if the resulted result is the result we are expecting.

Furthermore, an event listener is attached on the button view. When it is click, it will execute the code below. It is use to launch the camera image capture.

Intent photoCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(photoCaptureIntent, requestCode);

The result returned from this method will be access in the override method of the Activity class called onActivityResult(int requestCode, int resultCode, Intent data)

We have create three private method which will assist in storing and retrieving our Image to the SD card. The currentDateFormat()  method is used to generate the current time information which will be append to the image file name so that each image will have a unique name.

storeCameraPhotoInSDCard(Bitmap bitmap, String currentDate)   is the method responsible in using the bitmap object and the current time part of the file name and store it in the SD card.

The complete code for the class is shown below.


import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends ActionBarActivity {

    private ImageView imageHolder;
    private final int requestCode = 20;

    protected void onCreate(Bundle savedInstanceState) {

        imageHolder = (ImageView)findViewById(;
        Button capturedImageButton = (Button)findViewById(;
        capturedImageButton.setOnClickListener( new View.OnClickListener() {
            public void onClick(View v) {
                Intent photoCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(photoCaptureIntent, requestCode);

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(this.requestCode == requestCode && resultCode == RESULT_OK){
            Bitmap bitmap = (Bitmap)data.getExtras().get("data");

            String partFilename = currentDateFormat();
            storeCameraPhotoInSDCard(bitmap, partFilename);

            // display the image from SD Card to ImageView Control
            String storeFilename = "photo_" + partFilename + ".jpg";
            Bitmap mBitmap = getImageFileFromSDCard(storeFilename);

    private String currentDateFormat(){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HH_mm_ss");
        String  currentTimeStamp = dateFormat.format(new Date());
        return currentTimeStamp;

    private void storeCameraPhotoInSDCard(Bitmap bitmap, String currentDate){
        File outputFile = new File(Environment.getExternalStorageDirectory(), "photo_" + currentDate + ".jpg");
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
    private Bitmap getImageFileFromSDCard(String filename){
        Bitmap bitmap = null;
        File imageFile = new File(Environment.getExternalStorageDirectory() + filename);
        try {
            FileInputStream fis = new FileInputStream(imageFile);
            bitmap = BitmapFactory.decodeStream(fis);
        } catch (FileNotFoundException e) {
        return bitmap;

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(, menu);
        return true;

    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == {
            return true;

        return super.onOptionsItemSelected(item);

Save the file and run your project. If everything works for you, the project will appear like this in your device.

Display Captured Photo in ImageView using Android Camera Application

You can download the code for this tutorial below. If you are having hard time downloading the tutorials, kindly contact me.

Remember to subscribe with your email so that you will be among the first to receive our new post once it is published

One Response

Add a Comment