You are on page 1of 3

TALLER ANDROID

MANEJO DE LA CAMARA
1. En este ejemplo vamos a tomar una foto y guardarla en la raz de la
sdcard.
2. Cree el proyecto llamado TomarFotos. No usemos Fragmento es decir si
se crean los archivos activity_main.xml y fragment_main.xml, borrar el
primero y renombrar el segundo como el activity_main-xml. Despus
borrar las lneas del MainActivity.java en donde aparecen las referencias
a Fragment, es decir que slo quede el cdigo hasta
setContentView(activity_main.xml). Adems en los estilos quitar la
referencia a Theme.AppCompact.Ligth por @android:style/ThemeLigth. Adems en Propiedades del Proyecto quite la referencia a la
librera appcompact.
3. En el activity_main.xml coloque el siguiente cdigo:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/btnTomaFoto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="TOMAR FOTO" />
</RelativeLayout>

4. En el MainActivity.java coloque el siguiente cdigo:

import
import
import
import

java.io.File;
java.io.IOException;
java.util.Date;
java.text.SimpleDateFormat;

import
import
import
import
import
import
import

android.net.Uri;
android.os.Bundle;
android.os.Environment;
android.provider.MediaStore;
android.app.Activity;
android.content.Intent;
android.util.Log;

import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private final String ruta_fotos =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) +
"/misfotos/";
private File file = new File(ruta_fotos);
private Button boton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boton = (Button) findViewById(R.id.btnTomaFoto);
//Si no existe crea la carpeta donde se guardaran las fotos
file.mkdirs();
//accion para el boton
boton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String file = ruta_fotos + getCode() + ".jpg";
File mi_foto = new File( file );
try {
mi_foto.createNewFile();
} catch (IOException ex) {
Log.e("ERROR ", "Error:" + ex);
}
Uri uri = Uri.fromFile( mi_foto );
//Abre la camara para tomar la foto
Intent cameraIntent = new
Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//Guarda imagen
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
//Retorna a la actividad
startActivityForResult(cameraIntent, 0);
}
});
//====== codigo nuevo:end ======
}
private String getCode()
{
SimpleDateFormat dateFormat = new
SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date() );
String photoCode = "pic_" + date;
return photoCode;
}

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

5. Dar los permisos en el AndroidManifest.xml:


<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

6. Para revisar si la foto fue gaurdada vaya a la vista DDMS, seleccione


FileExplorer y busque en storage/Pictures/misfotos, all deben aparecer
las fotos que este tomando.

You might also like