Capture The Image, Saved it in SDCard and Show it in to the Screen in Android

For saving the image in SDCard we have to first check that “is SDCard available or not?”

For checking the SDCard availability in android:

public static boolean isSdPresent()
{
      return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}

and The code for the image catpure and show is:

import java.io.ByteArrayOutputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.alishaik.capturedemo.common.FindSDCard;

public class StartUpActivity extends Activity
{
      private Button captureButton;
      private ImageView showImage;
      private Bitmap bitmap;
      @Override
      public void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);

         if(FindSDCard.isSdPresent())
         {
             showImage=(ImageView)findViewById(R.id.captured_img);

              captureButton=(Button)findViewById(R.id.button1);
              captureButton.setText("Open Camera");
              captureButton.setOnClickListener(new Button.OnClickListener()
              {
                  @Override
                  public void onClick(View v)
                  {
                      startCamera();
                  }
             });
         }
        else
        {
            Toast.makeText(this,"Insert SDCard", Toast.LENGTH_LONG).show();
        }
    }

    private void startCamera()
    {
       Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
       intent.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI.getPath());
       startActivityForResult(intent, 1);
   }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode)
        {
           case -1:     Log.i("StartUpActivity", "Photo Captured");
                        bitmap=(Bitmap) data.getExtras().get("data");
                        MediaStore.Images.Media.insertImage(getContentResolver(),bitmap,null,null);
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                        showImage.setImageBitmap(bitmap);
                        break;
          default:      Log.i("StartUpActivity", "Cancelled Camera"); break;
      }
   }

    @Override
    public void onBackPressed()
    {
       super.onBackPressed();
    }

    public static boolean isSdPresent()
    {
        return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
    }
}

and study this link:

http://developer.android.com/training/camera/photobasics.html#Taking Photos Simply

Advertisements

2 responses to this post.

  1. Posted by Abhishek on February 5, 2016 at 14:41

    I don’t found FindSDCard in this example

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: