Share
Embed
(Demo Slides) Android: Cámara, Galería, Google Photos
Slides de ejemplo del curso profesional de desarrollo de aplicaciones Android.
Daniel Alvarez
July 01, 2016
Transcript
20 slides
-
Cámara Existen dos opciones para manejar la cámara, vía Intent
y gestión y personalización de la cámara. Nueva API para manejar la cámara android.hardware.camera2 compatible desde la API 21 (Android 5.0) 2 . 2 http://alvarez.tech -
Permisos Permiso para usar la cámara <uses-permission android:name="android.permission.CAMERA" /> <uses-permission
android:name="android.permission.RECORD_AUDIO" /> Si se guardará en memoria externa (SD) <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> Para grabar audio en un vídeo Si se quiere guardar la localización en la fotos <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-feature android:name="android.hardware.location.gps" /> 2 . 3 http://alvarez.tech -
Features Exigir tener cámara <uses-feature android:name="android.hardware.camera" android:required="true" /> Lista de
Features 2 . 4 http://alvarez.tech -
Tomar una foto Intent para tomar una foto Intent intent
= new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (intent.resolveActivity(getPackageManager()) != null) { startActivityForResult(intent, 777); } Obtener el resultado @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 777 && resultCode == RESULT_OK) { Bundle extras = data.getExtras(); Bitmap imageBitmap = (Bitmap) extras.get("data"); fotoImageView.setImageBitmap(imageBitmap); } } 3 . 2 http://alvarez.tech -
Tomar una foto y guardarla Intent de captura con parámetro
de guardado Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (intent.resolveActivity(getPackageManager()) != null) { File imagenFile = crearImagenFile(); if (imagenFile != null) { intent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(this, "tech.alvarez.testcamara.fileprovider", imagenFile)); startActivityForResult(intent, 888); } } 3 . 3 http://alvarez.tech -
Tomar una foto y guardarla Método para armar el File
(ruta y nombre) private File crearImagenFile() { try { String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String nombreImagen = "IMAGEN_" + timeStamp + "_"; File directorio = getExternalFilesDir(Environment.DIRECTORY_PICTURES); File imagenFile = File.createTempFile(nombreImagen, ".jpg", directorio); fotoPath = imagenFile.getAbsolutePath(); return imagenFile; } catch (IOException e) { return null; } } 3 . 4 http://alvarez.tech -
Tomar una foto y guardarla Provider donde colocamos la ruta
<provider android:name="android.support.v4.content.FileProvider" android:authorities="tech.alvarez.testcamara.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> <?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="mis_imagenes" path="Android/data/tech.alvarez.testcamara/files/Pictures" /> </paths> XML donde se encuentra el valor de la ruta 3 . 5 http://alvarez.tech -
Tomar una foto y guardarla private void colocarFoto() { int
anchoImageView = fotoImageView.getWidth(); int altoImageView = fotoImageView.getHeight(); BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = true; BitmapFactory.decodeFile(fotoPath, bmOptions); int anchoFoto = bmOptions.outWidth; int altoFoto = bmOptions.outHeight; int scaleFactor = 1; if ((anchoImageView > 0) || (altoImageView > 0)) { scaleFactor = Math.min(anchoFoto / anchoImageView, altoFoto / altoImageView); } bmOptions.inJustDecodeBounds = false; bmOptions.inSampleSize = scaleFactor; Bitmap bitmap = BitmapFactory.decodeFile(fotoPath, bmOptions); fotoImageView.setImageBitmap(bitmap); } private void obtenerFotoDeArchivo() { if (fotoPath != null) { colocarFoto(); fotoPath = null; } } 3 . 6 http://alvarez.tech -
Obtener una imagen Desde aplicaciones de galería (incluye Google Photos)
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(Intent.createChooser(intent, "Selecciona"), 123); Desde documentos, filtrando solo imágenes. Todas las opciones de aplicaciones (Dropbox, Google Drive, etc) Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); startActivityForResult(Intent.createChooser(intent, "Selecciona"), 123); Creando un chooser hacemos que siempre se nos presente las aplicaciones soportadas. 4 . 2 http://alvarez.tech -
Manejar resultado Cadenas de texto que se mostrarán en la
aplicación. @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 123 && resultCode == RESULT_OK && data != null) { Uri imagenUri = data.getData(); fotoImageView.setImageURI(imagenUri); } else { Toast.makeText(this, "No se selecciono ninguna imagen", Toast.LENGTH_SHORT).show(); } } 4 . 3 http://alvarez.tech -
Ejercicio SUBIR UNA IMAGEN A LA NUBE 5 . 1
http://alvarez.tech -
Integrar Firebase en Android Android Studio 2.2 incorpora la herramienta
para usar los servicios de Firebase de forma sencilla, se debe iniciar sesión con su cuenta Google. 5 . 3 http://alvarez.tech -
Subir archivo sin autenticación Reglas de Firebase Storage console.firebase.google.com 5
. 4 http://alvarez.tech