Skip to content

Instantly share code, notes, and snippets.

@MacoChave
Created November 26, 2017 20:13
Show Gist options
  • Save MacoChave/0be399171fe12c38bdd278cd25e3455a to your computer and use it in GitHub Desktop.
Save MacoChave/0be399171fe12c38bdd278cd25e3455a to your computer and use it in GitHub Desktop.
Capturar imagen desde camara
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<protected-broadcast android:name="android.intent.action.MEDIA_MOUNTED" />
private Uri IMAGE_URI;
private final int PHOTO_CODE = 200;
private final int PICK_IMAGE = 300;
public void selector() {
final AlertDialog.Builder builder;
final CharSequence[] opcion = {
"Tomar foto",
"Seleccionar foto"
};
builder = new AlertDialog.Builder(getContext());
builder.setTitle("Elige una opción");
builder.setItems(opcion, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (opcion[i] == "Tomar foto") {
abrirCamara();
} else if (opcion[i] == "Seleccionar foto") {
Intent gallery = new Intent(
Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
gallery.setType("image/*");
startActivityForResult(
gallery.createChooser(gallery, "Seleccionar imagen"),
PICK_IMAGE
);
}
}
});
builder.show();
}
private void abrirCamara() {
String MEDIA_DIRECTORY = "Tupperware/";
File file = new File(Environment.getExternalStorageDirectory(), MEDIA_DIRECTORY);
boolean isDirectoryCreated = file.exists();
if (isDirectoryCreated)
isDirectoryCreated = file.mkdirs();
if (isDirectoryCreated) {
Long timestamp = System.currentTimeMillis() / 1000;
String name = timestamp.toString() + ".jpg";
filename = Environment.getExternalStorageDirectory() + File.separator + MEDIA_DIRECTORY + File.separator + name;
File nuevo = new File(filename);
Intent camara = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
camara.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(nuevo));
startActivityForResult(camara, PHOTO_CODE);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
switch (requestCode)
{
case PHOTO_CODE:
MediaScannerConnection.scanFile(
getContext(),
new String[]{filename},
null,
new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String s, Uri uri) {
Log.i("ExternalStorage", "onScanCompleted: " + filename + ":");
Log.i("ExternalStorage", "onScanCompleted: uri: " + uri);
}
}
);
Bitmap bitmap = BitmapFactory.decodeFile(filename);
imgImagen.setImageBitmap(bitmap);
break;
case PICK_IMAGE:
IMAGE_URI = data.getData();
Log.i("IMAGE", "onActivityResult: " + IMAGE_URI.getPath());
imgImagen.setImageURI(IMAGE_URI);
break;
default:
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment