Android Tutorial – Camera and Gallery

Android provides very powerful Camera features which helps developer to build wide range of applications. In this tutorial we will see how to take photos using camera and place it on Imageview as well as selecting pictures from gallery.

There are two ways to work on Camera,

  • Camera API
  • Camera Intent

Let us see Camera Intent example of camera and gallery.

Step 1 – Create a new android project

Step 2 – In activity_main.xml, create on ImageView to display images captured by camera or gallery selected image. Along with ImageView, add two buttons. These buttons can be used to capture image from camera and select gallery image.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity"
>
<ImageView
android:id="@+id/imgView"
android:layout_marginTop="30dp"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/img"
>
</ImageView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:id="@+id/lin1"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:layout_below="@+id/imgView"
android:layout_alignParentBottom="true"
>

<Button
android:id="@+id/cameraButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:background="#5a9dda"
android:textColor="#ffffff"
android:text="Take a Photo"
>
</Button>
<Button
android:id="@+id/galleryButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAllCaps="false"
android:background="#5a9dda"
android:textColor="#ffffff"
android:text="Select Photo"
>
</Button>
</LinearLayout>
</LinearLayout>


In MainActivity.java class, two intents are created to capture image and to select image.
public class MainActivity extends AppCompatActivity {
private static final int CAMERA_REQUEST = 100;
private static final int SELECT_PICTURE =200;
ImageView imageView;
Button cameraButton,galleryButton;
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

imageView = (ImageView)this.findViewById(R.id.imgView);
cameraButton = (Button)this.findViewById(R.id.cameraButton);
galleryButton = (Button) findViewById(R.id.galleryButton);

cameraButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// Intent to capture image
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);

}
});

galleryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
//intent to select image
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,   "Select Picture"), SELECT_PICTURE);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
// Get the path from the Uri
final String path = getPathFromURI(selectedImageUri);
if (path != null) {
File f = new File(path);
selectedImageUri = Uri.fromFile(f);
}
// Set the image in ImageView
imageView.setImageURI(selectedImageUri);
}

} catch (Exception e) {
Log.e("ImageActivity", "Image capture or select error", e);
}
}

public String getPathFromURI(Uri contentUri) {
String res = null;
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
if (cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res = cursor.getString(column_index);
}
cursor.close();
return res;
}

}

For our expertise and help in app development services, please get in touch with http://purplechai.com/

Leave a Reply

Your email address will not be published. Required fields are marked *