Native Mobile App Development With Java

Take a picture from the camera set in the imageView Android

10-Oct-2024

Take a picture from the camera set in the imageView Android

Output:











1/ Modify your 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">

<ImageView
android:id="@+id/imgCamera"
android:layout_width="400dp"
android:layout_height="400dp"
android:scaleType="fitXY"/>


<Button
android:id="@+id/btnCamera"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="21dp"
android:text="Open Camera"/>


</LinearLayout>



2/Modify your MainActivity.java .


package com.tutorialb.takeapicturefromthecamera;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class MainActivity extends AppCompatActivity {

private final int CAMERA_REQ_CODE = 100;

ImageView imgViewCamera;
Button BTNcamera;

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

imgViewCamera = findViewById(R.id.imgCamera);
BTNcamera = findViewById(R.id.btnCamera);

BTNcamera.setOnClickListener(new View.OnClickListener() {
@SuppressLint("QueryPermissionsNeeded")
@Override
public void onClick(View v) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, CAMERA_REQ_CODE);
}
}
});
}

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

if (resultCode == RESULT_OK && requestCode == CAMERA_REQ_CODE) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = null;
if (extras != null) {
imageBitmap = (Bitmap) extras.get("data");
}


imgViewCamera.setImageBitmap(imageBitmap);

// Save the image to the gallery
if (imageBitmap != null) {
saveImageToGallery(imageBitmap);
}
}
}

private void saveImageToGallery(Bitmap bitmap) {
OutputStream fos;
try {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {

ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/MyAppImages");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.Images.Media.IS_PENDING, true);

Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
fos = getContentResolver().openOutputStream(uri);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();

values.put(MediaStore.Images.Media.IS_PENDING, false);
getContentResolver().update(uri, values, null, null);
Toast.makeText(this, "Image saved to gallery", Toast.LENGTH_SHORT).show();
} else {

String imagesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + "/MyAppImages";
File file = new File(imagesDir);
if (!file.exists()) {
file.mkdirs();
}

File imageFile = new File(file, System.currentTimeMillis() + ".jpg");
fos = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();


Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(imageFile);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);

Toast.makeText(this, "Image saved to gallery", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
}



}
}




Comments