21.04.2014 Views

Curso-Android-Desarrollo-de-Aplicaciones-Móviles

Curso-Android-Desarrollo-de-Aplicaciones-Móviles

Curso-Android-Desarrollo-de-Aplicaciones-Móviles

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CURSO ANDROID<br />

51<br />

Si quisiéramos incluir esa imagen en nuestra galería, utilizamos un MediaScannerConnectionClient 1 .<br />

new MediaScannerConnectionClient() {<br />

private MediaScannerConnection msc = null; {<br />

msc = new MediaScannerConnection(getApplicationContext(), this);<br />

msc.connect();<br />

}<br />

public void onMediaScannerConnected() {<br />

msc.scanFile(fileName, null);<br />

}<br />

public void onScanCompleted(String path, Uri uri) {<br />

msc.disconnect();<br />

}<br />

};<br />

Si viene <strong>de</strong> la galería recibimos el URI <strong>de</strong> la imagen y construimos un Bitmap a partir <strong>de</strong> un stream <strong>de</strong><br />

bytes:<br />

Uri selectedImage = data.getData();<br />

InputStream is;<br />

try {<br />

is = getContentResolver().openInputStream(selectedImage);<br />

BufferedInputStream bis = new BufferedInputStream(is);<br />

Bitmap bitmap = BitmapFactory.<strong>de</strong>co<strong>de</strong>Stream(bis);<br />

ImageView iv = (ImageView)findViewById(R.id.imgView);<br />

iv.setImageBitmap(bitmap);<br />

} catch (FileNotFoundException e) {}<br />

Si estamos utilizando la cámara la vista será <strong>de</strong> la siguiente forma:<br />

1 http://<strong>de</strong>veloper.android.com/reference/android/media/MediaScannerConnection.MediaScannerConnectionClient.<br />

html

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!