Media Store Camera Intent : Camera « Hardware « Android






Media Store Camera Intent

   
package app.test;

import java.io.FileNotFoundException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.provider.MediaStore.Images.Media;
import android.content.ContentValues;

public class Test extends Activity {

  final static int CAMERA_RESULT = 0;

  Uri imageFileUri;

  ImageView returnedImageView;
  Button takePictureButton;
  Button saveDataButton;
  TextView titleTextView;
  TextView descriptionTextView;
  EditText titleEditText;
  EditText descriptionEditText;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    returnedImageView = (ImageView) findViewById(R.id.ReturnedImageView);
    takePictureButton = (Button) findViewById(R.id.TakePictureButton);
    saveDataButton = (Button) findViewById(R.id.SaveDataButton);
    titleTextView = (TextView) findViewById(R.id.TitleTextView);
    descriptionTextView = (TextView) findViewById(R.id.DescriptionTextView);
    titleEditText = (EditText) findViewById(R.id.TitleEditText);
    descriptionEditText = (EditText) findViewById(R.id.DescriptionEditText);

    returnedImageView.setVisibility(View.GONE);
    saveDataButton.setVisibility(View.GONE);
    titleTextView.setVisibility(View.GONE);
    descriptionTextView.setVisibility(View.GONE);
    titleEditText.setVisibility(View.GONE);
    descriptionEditText.setVisibility(View.GONE);

    takePictureButton.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        imageFileUri = getContentResolver().insert(
            Media.EXTERNAL_CONTENT_URI, new ContentValues());
        Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
            imageFileUri);
        startActivityForResult(i, CAMERA_RESULT);
      }
    });

    saveDataButton.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        ContentValues contentValues = new ContentValues(3);
        contentValues.put(Media.DISPLAY_NAME, titleEditText.getText().toString());
        contentValues.put(Media.DESCRIPTION, descriptionEditText.getText().toString());
        getContentResolver().update(imageFileUri, contentValues, null,null);
        Toast bread = Toast.makeText(Test.this,"Record Updated", Toast.LENGTH_SHORT);
        bread.show();
        takePictureButton.setVisibility(View.VISIBLE);
        returnedImageView.setVisibility(View.GONE);
        saveDataButton.setVisibility(View.GONE);
        titleTextView.setVisibility(View.GONE);
        descriptionTextView.setVisibility(View.GONE);
        titleEditText.setVisibility(View.GONE);
        descriptionEditText.setVisibility(View.GONE);
      }
    });
  }

  protected void onActivityResult(int requestCode, int resultCode,
      Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    if (resultCode == RESULT_OK) {
      takePictureButton.setVisibility(View.GONE);
      saveDataButton.setVisibility(View.VISIBLE);
      returnedImageView.setVisibility(View.VISIBLE);
      titleTextView.setVisibility(View.VISIBLE);
      descriptionTextView.setVisibility(View.VISIBLE);
      titleEditText.setVisibility(View.VISIBLE);
      descriptionEditText.setVisibility(View.VISIBLE);

      int dw = 200; // Make it at most 200 pixels wide
      int dh = 200; // Make it at most 200 pixels tall

      try {
        BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
        bmpFactoryOptions.inJustDecodeBounds = true;
        Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(
                imageFileUri), null, bmpFactoryOptions);

        int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight/ (float) dh);
        int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth/ (float) dw);
        if (heightRatio > 1 && widthRatio > 1) {
          if (heightRatio > widthRatio) {
            bmpFactoryOptions.inSampleSize = heightRatio;
          } else {
            bmpFactoryOptions.inSampleSize = widthRatio;
          }
        }
        bmpFactoryOptions.inJustDecodeBounds = false;
        bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(
                imageFileUri), null, bmpFactoryOptions);

        returnedImageView.setImageBitmap(bmp);
      } catch (FileNotFoundException e) {
        Log.v("ERROR", e.toString());
      }
    }
  }
}
//layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
  <ImageView android:id="@+id/ReturnedImageView" 
             android:layout_width="wrap_content" 
             android:layout_height="wrap_content">
    </ImageView>
    <TextView android:layout_width="wrap_content" 
              android:layout_height="wrap_content" 
              android:text="Title:" 
              android:id="@+id/TitleTextView">
    </TextView>
    <EditText android:layout_height="wrap_content" 
              android:id="@+id/TitleEditText" 
              android:layout_width="fill_parent">
    </EditText>
    <TextView android:layout_width="wrap_content" 
              android:layout_height="wrap_content" 
              android:text="Description" 
              android:id="@+id/DescriptionTextView">
    </TextView>
    <EditText android:layout_height="wrap_content" 
              android:layout_width="fill_parent" 
              android:id="@+id/DescriptionEditText">
    </EditText>
    <Button android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:id="@+id/TakePictureButton" 
            android:text="Take Picture">
    </Button>
    <Button android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:id="@+id/SaveDataButton" 
            android:text="Save Data">
    </Button>
</LinearLayout>

   
    
    
  








Related examples in the same category

1.Camera preview
2.Camera With Intent
3.Camera Intent
4.Using Timer to control Camera
5.Camera orientation
6.Camera Preview and Camera.getNumberOfCameras()
7.Take a snapshot
8.Take and preview Photo
9.A grid that displays a set of framed photos.