January 2013
Intermediate to advanced
408 pages
9h 23m
English
Most Android phones nowadays include built-in cameras — one at the front and one at the back. This recipe shows you how to programmatically invoke the camera to take a picture and then save the picture to external storage.
First, add a Button in your application UI, such as activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="btnTakePhoto"
android:text="Take Photo" />
</LinearLayout>
In the activity, code the following lines in bold:
package net.learn2develop.camera;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
static int TAKE_PICTURE = 1;
Uri outputFileUri; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, ...Read now
Unlock full access