This tutorial is a good example of how to use a photo camera with Android Studio.
Start the default project named BitCam and change these files:
The BitCam.java file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | package org.free_tutorials.catafest.bitcam; import android.content.Intent; import android.graphics.Bitmap; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class BitCam extends AppCompatActivity { ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_bit_cam); Button btnCamera = (Button) findViewById(R.id.btnCamera); ImageView imageView = (ImageView) findViewById(R.id.imageView); btnCamera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view){ Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent,0); } }); } @Override public void onActivityReenter(int resultCode, Intent data) { super.onActivityReenter(resultCode, data); Bitmap bitmap = (Bitmap)data.getExtras().get("data"); imageView.setImageBitmap(bitmap); } } |
The activity_bit_cam.xml file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="org.free_tutorials.catafest.bitcam.BitCam" tools:layout_editor_absoluteX="0dp" tools:layout_editor_absoluteY="81dp"> <ImageView android:id="@+id/imageView" android:layout_width="465dp" android:layout_height="644dp" android:layout_weight="1" android:scaleType="fitXY" android:visibility="visible" tools:layout_editor_absoluteX="2dp" tools:layout_editor_absoluteY="-3dp" /> <Button android:id="@+id/btnCamera" android:layout_width="324dp" android:layout_height="55dp" android:background="@android:color/holo_red_dark" android:text="Open camera" android:textColor="@android:color/white" tools:layout_editor_absoluteX="72dp" tools:layout_editor_absoluteY="556dp" /> </android.support.constraint.ConstraintLayout> |
The result is an android application with a button.
When you press the button the photo camera start.