Skip to content

Instantly share code, notes, and snippets.

@marcherdiego
Last active October 27, 2016 21:58
Show Gist options
  • Save marcherdiego/bde9ba8986db080d33b37863b39e240e to your computer and use it in GitHub Desktop.
Save marcherdiego/bde9ba8986db080d33b37863b39e240e to your computer and use it in GitHub Desktop.
Taller parte 2: Uso de la cámara
<Button
android:text="Tomar Foto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/button2"
android:onClick="tomarFoto"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
app:srcCompat="@android:color/darker_gray"
android:layout_below="@+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/imageView"/>
<manifest ... >
<uses-feature android:name="android.hardware.camera"
android:required="true" />
...
</manifest>
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
public class CameraActivity extends AppCompatActivity {
private static final int IMAGE_FROM_CAMERA = 1;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
imageView = ((ImageView) findViewById(R.id.imageView));
}
...
public void tomarFoto(View view) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, IMAGE_FROM_CAMERA);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == IMAGE_FROM_CAMERA && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment