Take a picture

Description

The following code shows how to Take a picture.

Example


<?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"
    >
<TextView  
  android:id="@+id/main_text_view"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Click the button below to take a picture"
  android:textSize="30dip"
    />
<Button
  android:id="@+id/camera_button"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Take a picture" 
  android:textSize="30dip"/>
</LinearLayout>

Java code


import android.app.Activity;
import android.os.Bundle;
//from  www.jav a  2  s .c  o m
import android.view.View;
import android.widget.Button;
import android.content.Intent;
import android.provider.MediaStore;

import android.view.View.OnClickListener;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;


/*

Written by Martin Dinstuhl
mdinstuhl@gmail.com

I put this together in order to help other Android developers 
who might be having the same problem I've
had for the past few days.  Please let me know if you have any questions.

*/

public class MainActivity extends Activity
{
  
  final int PICTURE_ACTIVITY = 1; 
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    final Button cameraButton = (Button)findViewById(R.id.camera_button); 
    cameraButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v){
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
        startActivityForResult(cameraIntent, PICTURE_ACTIVITY); 
      }
    });
    }
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    
    AlertDialog msgDialog;
    if (resultCode == RESULT_CANCELED) { // The user didn't like the photo.  ;_;
        msgDialog = createAlertDialog("Q_Q", "", "OK!  I'LL TRY AGAIN!");
    } else {
      msgDialog = createAlertDialog("ZOMG!", "CRAP!", "I KNOW RITE??!?");
    }
    msgDialog.show();
  }
  
  private AlertDialog createAlertDialog(String title, String msg, String buttonText){
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    AlertDialog msgDialog = dialogBuilder.create();
    msgDialog.setTitle(title);
    msgDialog.setMessage(msg);
    msgDialog.setButton(buttonText, new DialogInterface.OnClickListener(){
      @Override
      public void onClick(DialogInterface dialog, int idx){
        return; // Nothing to see here...
      }
    });
    
    return msgDialog;
  }

}




















Home »
  Android »
    Android Basics »




Hello Android
Resources
Activity
Calendar
Camera
Contact
Content Provider
Database
Hardware
Intent
Location
Media
Network
Notification
Preference
Sensor
Service
SMS