Take Photo From Camera And Gallery In Android Example
In this article, we will discuss how to take a photo from the camera and gallery in an Android application. Taking images is an essential part of Android development, and the process of taking an image is different from one device to another. Therefore, we will provide you with an example that works on most Android devices.
1. Preparing Your Android Environment
To start, you need to have the Android Studio installed on your computer. If you do not have it installed, you can download it from the official website. Once installed, create a new project in the Android Studio, and name it "TakePhotoExample".
Next, add the following dependencies to your project's build.gradle file:
dependencies {implementation 'com.android.support:appcompat-v7:28.0.0'implementation 'com.android.support:design:28.0.0'implementation 'com.android.support:support-v4:28.0.0'implementation 'com.squareup.picasso:picasso:2.5.2'}
These dependencies are required for the image loading and displaying.
2. Creating the Layout
The next step is to create the layout for your application. To do this, you can use the following code snippet:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:id="@+id/btn_take_photo"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Take Photo"android:layout_centerInParent="true"/><Buttonandroid:id="@+id/btn_choose_photo"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Choose Photo"android:layout_below="@id/btn_take_photo"android:layout_marginTop="10dp"android:layout_centerHorizontal="true"/></RelativeLayout>
As you can see, the layout consists of two buttons, which allow the user to take a photo or choose one from the gallery. Place this code in the activity_main.xml file.
3. Requesting Permissions
Before you can start taking photos, you need to request the necessary permissions from the user. In this case, you need to request the camera and storage permissions. Add the following code to your MainActivity.java file:
private static final int PERMISSION_REQUEST_CODE = 200;private void requestPermission() {ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);}@Overridepublic void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {switch (requestCode) {case PERMISSION_REQUEST_CODE:if (grantResults.length > 0) {boolean cameraPermission = grantResults[0] == PackageManager.PERMISSION_GRANTED;boolean storagePermission = grantResults[1] == PackageManager.PERMISSION_GRANTED;if (cameraPermission && storagePermission) {Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_SHORT).show();} else {Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();requestPermission();}}break;}}
This code requests the necessary permissions from the user, and if they are granted, it displays a message to the user. Otherwise, it requests the permissions again.
4. Taking a Photo from the Camera
To take a photo from the camera, you need to create an intent and start it. Add the following code to your MainActivity.java file:
private static final int REQUEST_CAMERA = 1;private void takePhotoFromCamera() {Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);startActivityForResult(intent, REQUEST_CAMERA);}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK) {Bitmap bitmap = (Bitmap) data.getExtras().get("data");ImageView imageView = findViewById(R.id.image_view);imageView.setImageBitmap(bitmap);}}
This code creates an intent to capture an image from the camera, and then starts it. When the user takes the image, the onActivityResult() method receives the data from the camera and sets it to the ImageView.
5. Choosing a Photo from the Gallery
To choose a photo from the gallery, you need to create an intent and start it. Add the following code to your MainActivity.java file:
private static final int REQUEST_GALLERY = 2;private void choosePhotoFromGallery() {Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);startActivityForResult(intent, REQUEST_GALLERY);}
This code creates an intent to pick an image from the gallery, and then starts it. When the user selects an image, the onActivityResult() method receives the data from the gallery and sets it to the ImageView.
6. Displaying the Image
To display the image, we will use the Picasso library. Before using it, add the dependency as mentioned in the first step. After that, add the following code to your MainActivity.java file:
private ImageView imageView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED &&ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {requestPermission();}imageView = findViewById(R.id.image_view);Button btnTakePhoto = findViewById(R.id.btn_take_photo);btnTakePhoto.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {takePhotoFromCamera();}});Button btnChoosePhoto = findViewById(R.id.btn_choose_photo);btnChoosePhoto.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {choosePhotoFromGallery();}});}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK) {Bitmap bitmap = (Bitmap) data.getExtras().get("data");imageView.setImageBitmap(bitmap);} else if (requestCode == REQUEST_GALLERY && resultCode == RESULT_OK) {Uri uri = data.getData();Picasso.with(MainActivity.this).load(uri).into(imageView);}}
This code sets the image to the ImageView using the Picasso library. When the user takes a photo or chooses one from the gallery, it gets loaded into the ImageView with the help of the library.
Conclusion
In this article, we have discussed how to take a photo from the camera and gallery in an Android application. We covered the steps required to prepare the environment, create the layout, request permissions, take a photo from the camera, choose a photo from the gallery, and display the image. By following these steps, you should be able to implement the process of taking photos into your Android application.