In Android app development, the integration of camera and gallery features is very consumer-friendly and is an essential functionality for many apps. In this tutorial, we will develop a simple camera and gallery integration app using Java. This app will allow users to take photos or select images from the gallery. Now, let’s look at the step-by-step process required to create this app.
1. Environment Setup
Install Android Studio and create a new project. When creating the project, select “Empty Activity” or “Basic Activity.” Choose Java as the language and click “Finish” to complete the project creation.
2. Request Necessary Permissions
You need to add the required permissions to the AndroidManifest.xml file to run the camera and gallery functions.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.camera_gallery">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
3. UI Design
Design a simple user interface in the activity_main.xml file. Add two buttons and an ImageView to display the image.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button_camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take a Photo with Camera"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>
<Button
android:id="@+id/button_gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select from Gallery"
android:layout_below="@id/button_camera"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_below="@id/button_gallery"
android:layout_marginTop="20dp"
android:scaleType="centerCrop"/>
</RelativeLayout>
4. Implement MainActivity.java
Now, write the MainActivity.java file to call the camera or gallery app based on button click events. First, set the click listeners for the buttons and implement the respective functionalities.
package com.example.camera_gallery;
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 androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final int CAMERA_REQUEST = 100;
private static final int GALLERY_REQUEST = 200;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.image_view);
Button buttonCamera = findViewById(R.id.button_camera);
Button buttonGallery = findViewById(R.id.button_gallery);
buttonCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
buttonGallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, GALLERY_REQUEST);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == CAMERA_REQUEST) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
} else if (requestCode == GALLERY_REQUEST) {
Uri selectedImageUri = data.getData();
imageView.setImageURI(selectedImageUri);
}
}
}
}
5. Run and Test the App
Now that you’ve written all the code, run the app to test it. When you run the app on an emulator or a real smartphone, pressing the “Take a Photo with Camera” button will open the camera app, allowing you to take a photo and set it as the preview image. Pressing the “Select from Gallery” button allows you to choose an image from the gallery app and display it as a preview.
6. Conclusion
In this tutorial, we developed a simple Android app that integrates the camera and gallery using Java. This functionality is a fundamental skill that can be used in various Android apps, and you can build upon this to add more complex features tailored to individual needs. It is recommended to continue expanding on these foundational elements as you progress in Android development.