एंड्रॉइड में कैमरा इंटेंट से फोटो लेना — पूरा गाइड
एंड्रॉइड में फोटो लेने के दो मुख्य तरीके हैं:
- इंटेंट (ACTION_IMAGE_CAPTURE) — सिस्टम कैमरा ऐप खुलता है, यूज़र फोटो लेता है, रिजल्ट आपके ऐप को मिलता है। **सबसे सरल, परमिशन कम, कोई UI कोड नहीं।**
- CameraX / Camera2 API — अपने ऐप में कैमरा प्रीव्यू, कंट्रोल, कैप्चर सब कुछ। **जटिल, पूर्ण नियंत्रण, परमिशन ज्यादा।**
यह ट्यूटोरियल **विधि 1 (इंटेंट)** कवर करता है — शुरुआत के लिए बेस्ट।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio → File → New → New Project → "Empty Activity" → Finish।
चरण 2: Permissions (AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.camerademo">
<!-- कैमरा हार्डवेयर फीचर (फिल्टर प्ले स्टोर पर) -->
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
<!-- Android 10+ (API 29) में कैमरा परमिशन रनटाइम मांगी जाती है, मैनिफेस्ट में सिर्फ डिक्लेयर -->
<uses-permission android:name="android.permission.CAMERA" />
<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.CameraDemo">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
चरण 3: Layout (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<Button
android:id="@+id/btnTakePhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:text="फोटो लें"
android:textSize="18sp"
android:padding="12dp 24dp" />
<ImageView
android:id="@+id/ivPhoto"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@id/btnTakePhoto"
android:layout_marginTop="16dp"
android:layout_alignParentBottom="true"
android:scaleType="centerCrop"
android:contentDescription="कैप्चर की गई फोटो"
android:background="#f0f0f0" />
</RelativeLayout>
चरण 4: MainActivity.java — रनटाइम परमिशन + इंटेंट
package com.example.camerademo;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CAMERA_PERMISSION = 100;
private static final int REQUEST_IMAGE_CAPTURE = 101;
private Button btnTakePhoto;
private ImageView ivPhoto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnTakePhoto = findViewById(R.id.btnTakePhoto);
ivPhoto = findViewById(R.id.ivPhoto);
btnTakePhoto.setOnClickListener(v -> {
if (checkCameraPermission()) {
dispatchTakePictureIntent();
} else {
requestCameraPermission();
}
});
}
// कैमरा परमिशन चेक
private boolean checkCameraPermission() {
return ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED;
}
// परमिशन रिक्वेस्ट
private void requestCameraPermission() {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
REQUEST_CAMERA_PERMISSION);
}
// परमिशन रिजल्ट हैंडल
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CAMERA_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
dispatchTakePictureIntent();
} else {
Toast.makeText(this, "कैमरा परमिशन जरूरी है", Toast.LENGTH_LONG).show();
}
}
}
// कैमरा इंटेंट लॉन्च करें
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
} else {
Toast.makeText(this, "कोई कैमरा ऐप नहीं मिला", Toast.LENGTH_SHORT).show();
}
}
// रिजल्ट हैंडल करें
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
// थंबनेल बिटमैप मिलता है (फुल-साइज़ इमेज के लिए FileProvider जरूरी)
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap imageBitmap = (Bitmap) extras.get("data");
ivPhoto.setImageBitmap(imageBitmap);
}
}
}
}
चरण 5: चलाएं और टेस्ट करें
- ऐप खोलें → "फोटो लें" दबाएं।
- पहली बार: कैमरा परमिशन डायलॉग आएगा → "Allow" चुनें।
- कैमरा ऐप खुलेगा → फोटो लें → "OK/सहेजें" दबाएं।
- ऐप पर वापस आएं → ImageView में थंबनेल दिखेगा।
📸 फुल-रिज़ॉल्यूशन इमेज के लिए: FileProvider उपयोग करें
डिफ़ॉल्ट इंटेंट केवल **थंबनेल** (छोटा बिटमैप) लौटाता है। फुल-साइज़ इमेज फाइल पाने के लिए:
1. FileProvider कॉन्फ़िगर करें (res/xml/file_paths.xml)
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="https://schemas.android.com/apk/res/android">
<external-files-path name="photos" path="Pictures/" />
</paths>
2. Manifest में Provider जोड़ें
<application ...>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
3. इंटेंट में URI पास करें
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// फोटो सेव करने के लिए फाइल बनाएं
File photoFile = createImageFile();
if (photoFile != null) {
Uri photoUri = FileProvider.getUriForFile(this,
getPackageName() + ".fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
return File.createTempFile(imageFileName, ".jpg", storageDir);
}
🚀 आधुनिक विकल्प: CameraX (Jetpack) — अनुशंसित
नए ऐप्स के लिए **CameraX** उपयोग करें — लाइफसाइकल-अवेयर, डिवाइस-कम्पैटिबल, सरल API:
// build.gradle
dependencies {
def camerax_version = "1.3.0"
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
implementation "androidx.camera:camera-view:${camerax_version}"
implementation "androidx.camera:camera-extensions:${camerax_version}"
}
// उपयोग (Kotlin)
val imageCapture = ImageCapture.Builder().build()
val photoFile = File(outputDir, "photo_${System.currentTimeMillis()}.jpg")
val outputOptions = ImageCapture.OutputFileOptions.Builder(photoFile).build()
imageCapture.takePicture(outputOptions, ContextCompat.getMainExecutor(this),
object : ImageCapture.OnImageSavedCallback {
override fun onImageSaved(output: ImageCapture.OutputFileResults) {
// सफल — photoFile में फुल इमेज
}
override fun onError(exc: ImageCaptureException) {
// त्रुटि
}
})
सारांश
- ✅ **सरल जरूरतों के लिए**:
ACTION_IMAGE_CAPTUREइंटेंट + रनटाइम परमिशन — 50 लाइन्स से कम कोड। - ✅ **थंबनेल मिलता है** — फुल इमेज के लिए FileProvider जरूरी।
- 🚀 **नए/गंभीर ऐप्स के लिए**: CameraX — प्रोफेशनल, रखरखाव योग्य, एक्सटेंसिबल।
- ⚠️ Camera2 API **लो-लेवल, जटिल** — केवल तभी जब CameraX पर्याप्त न हो।