Professional Documents
Culture Documents
usar la cmara
Multimedia
A.
para fotos
activity_main.xml
Nuestra interfaz estar
compuesta de un solo botn, el
cdigo XML es el siguiente:
<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="@string/strTomaFoto" />
</RelativeLayout>
ActivityMain.java
public class MainActivity extends ActionBarActivity {
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);
//======== codigo nuevo ========
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 ======
}
Permisos
Estos se agregan en el
AndroidManifest.xml, te permiten
manipular la cmara desde la aplicacin
y escribir en elsdcard.
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>