Android Programming

Hands On No. 10 : Working with Cameras and Images

Resources for setting Image from Camera

Source Code of MainAcivity.java


import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private static final int PERMISSION_CODE = 1000;
private static final int IMAGE_CAPTURE_CODE = 1001;

Button mCaptureBtn;
ImageView mImageView;

Uri image_uri;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mImageView = findViewById(R.id.image_view);
mCaptureBtn = findViewById(R.id.capture_image_btn);

//button click
mCaptureBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//if system os is >= marshmallow, request runtime permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if (checkSelfPermission(Manifest.permission.CAMERA) ==
PackageManager.PERMISSION_DENIED ||
checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
PackageManager.PERMISSION_DENIED){
//permission not enabled, request it
String[] permission = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
//show popup to request permissions
requestPermissions(permission, PERMISSION_CODE);
}
else {
//permission already granted
openCamera();
}
}
else {
//system os < marshmallow
openCamera();
}
}
});
}

private void openCamera() {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
values.put(MediaStore.Images.Media.DESCRIPTION, "From the Camera");
image_uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
//Camera intent
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, image_uri);
startActivityForResult(cameraIntent, IMAGE_CAPTURE_CODE);
}

//handling permission result
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//this method is called, when user presses Allow or Deny from Permission Request Popup
switch (requestCode){
case PERMISSION_CODE:{
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED){
//permission from popup was granted
openCamera();
}
else {
//permission from popup was denied
Toast.makeText(this, "Permission denied...", Toast.LENGTH_SHORT).show();
}
}
}
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//called when image was captured from camera

super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
//set the image captured to our ImageView
mImageView.setImageURI(image_uri);
}
}
}
Source Code of activity_main.xml
<?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"
android:gravity="center_horizontal"
tools:context=".MainActivity">


<ImageView
android:id="@+id/image_view"
android:scaleType="centerCrop"
tools:srcCompat="@tools:sample/avatars"
android:layout_width="200dp"
android:layout_height="200dp" />


<Button
android:id="@+id/capture_image_btn"
android:text="Capture Image"
style="@style/Base.Widget.AppCompat.Button.Colored"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>


Add Following permission in AndroidManifest.xml File

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Resources for setting Image from Gallery

Source Code of MainAcivity.java
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.IOException;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity2 extends AppCompatActivity {



final private int PICK_IMAGE_REQUEST = 1;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

Button selectImage = findViewById(R.id.capture_image_btn1);

selectImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chooseImage();
}
});
}

public void chooseImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

Uri uri = data.getData();

try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);


ImageView imageView = findViewById(R.id.image_view1);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Source Code of activity_main.xml
<?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"
android:gravity="center_horizontal"
tools:context=".MainActivity">


<ImageView
android:id="@+id/image_view1"
android:scaleType="centerCrop"
tools:srcCompat="@tools:sample/avatars"
android:layout_width="200dp"
android:layout_height="200dp" />


<Button
android:id="@+id/capture_image_btn1"
android:text="Change Image"
style="@style/Base.Widget.AppCompat.Button.Colored"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

Add Following permission in AndroidManifest.xml File

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>