Intent के माध्यम से इमेज रिसोर्स ID या बिटमैप पास करना
एंड्रॉइड में एक एक्टिविटी से दूसरी में इमेज पास करने के दो मुख्य तरीके हैं: रिसोर्स ID (इंट/ड्रॉएबल रेफरेंस) पास करना (कुशल, केवल ऐप-इंटरनल इमेज के लिए) या बिटमैप/बाइट ऐरे पास करना (किसी भी इमेज के लिए, लेकिन मेमोरी-इंटेंसिव)।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में प्रोजेक्ट बनाएं और res/drawable/ में एक इमेज रखें (जैसे sample_image.jpg या image.png)।
चरण 2: पहली एक्टिविटी लेआउट
res/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">
<ImageView
android:id="@+id/ivSourceImage"
android:layout_width="match_parent"
android:layout_height="250dp"
android:scaleType="centerCrop"
android:src="@drawable/sample_image"
android:contentDescription="स्रोत इमेज" />
<Button
android:id="@+id/btnSendResourceId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/ivSourceImage"
android:layout_marginTop="16dp"
android:onClick="sendByResourceId"
android:text="रिसोर्स ID द्वारा भेजें (कुशल)" />
<Button
android:id="@+id/btnSendBitmap"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btnSendResourceId"
android:layout_marginTop="8dp"
android:onClick="sendByBitmap"
android:text="बिटमैप द्वारा भेजें (बड़ी इमेज के लिए)" />
<Button
android:id="@+id/btnSendUri"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btnSendBitmap"
android:layout_marginTop="8dp"
android:onClick="sendByUri"
android:text="कंटेंट URI द्वारा भेजें (गैलरी इमेज)" />
</RelativeLayout>
चरण 3: MainActivity - तीन तरीके से इमेज भेजें
package com.app.sample;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.io.ByteArrayOutputStream;
public class MainActivity extends AppCompatActivity {
private static final int PICK_IMAGE_REQUEST = 1001;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// तरीका 1: रिसोर्स ID पास करें (सबसे कुशल - केवल ऐप रिसोर्सेज के लिए)
public void sendByResourceId(View view) {
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("image_type", "resource_id");
intent.putExtra("res_id", R.drawable.sample_image); // इंट मान
startActivity(intent);
}
// तरीका 2: बिटमैप पास करें (Parcelable - लेकिन ट्रांजैक्शन बफर लिमिट 1MB)
public void sendByBitmap(View view) {
// ड्रॉएबल से बिटमैप डिकोड करें
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sample_image);
// बड़ी इमेज के लिए स्केल डाउन करें (OOM से बचने के लिए)
Bitmap scaledBitmap = scaleBitmapIfNeeded(bitmap, 1024, 1024);
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("image_type", "bitmap");
intent.putExtra("bitmap", scaledBitmap); // Parcelable के रूप से पास होता है
startActivity(intent);
}
// तरीका 3: कंटेंट URI पास करें (गैलरी/कैमरा इमेज के लिए सर्वश्रेष्ठ)
public void sendByUri(View view) {
Intent pickIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
startActivityForResult(pickIntent, PICK_IMAGE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null) {
Uri imageUri = data.getData();
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("image_type", "uri");
intent.putExtra("image_uri", imageUri.toString()); // URI को स्ट्रिंग के रूप में पास करें
startActivity(intent);
}
}
// बिटमैप स्केलिंग हेल्पर (OOM रोकथाम)
private Bitmap scaleBitmapIfNeeded(Bitmap bitmap, int maxWidth, int maxHeight) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
if (width <= maxWidth && height <= maxHeight) return bitmap;
float scale = Math.min((float) maxWidth / width, (float) maxHeight / height);
return Bitmap.createScaledBitmap(bitmap,
(int) (width * scale), (int) (height * scale), true);
}
}
चरण 4: दूसरी एक्टिविटी लेआउट
res/layout/activity_second.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=".SecondActivity">
<TextView
android:id="@+id/tvImageInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:gravity="center"
android:textSize="16sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/ivReceivedImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:contentDescription="प्राप्त इमेज" />
</RelativeLayout>
चरण 5: SecondActivity - इमेज प्राप्त करें और प्रदर्शित करें
package com.app.sample;
import android.content.ContentResolver;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.io.InputStream;
public class SecondActivity extends AppCompatActivity {
private ImageView ivReceivedImage;
private TextView tvImageInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
ivReceivedImage = findViewById(R.id.ivReceivedImage);
tvImageInfo = findViewById(R.id.tvImageInfo);
handleReceivedImage();
}
private void handleReceivedImage() {
String imageType = getIntent().getStringExtra("image_type");
switch (imageType) {
case "resource_id":
handleResourceId();
break;
case "bitmap":
handleBitmap();
break;
case "uri":
handleUri();
break;
default:
tvImageInfo.setText("अज्ञात इमेज प्रकार");
}
}
private void handleResourceId() {
int resId = getIntent().getIntExtra("res_id", 0);
if (resId != 0) {
ivReceivedImage.setImageResource(resId);
tvImageInfo.setText("प्रकार: रिसोर्स ID\nरिसोर्स ID: " + resId);
} else {
tvImageInfo.setText("त्रुटि: वैध रिसोर्स ID नहीं मिला");
}
}
private void handleBitmap() {
Bitmap bitmap = getIntent().getParcelableExtra("bitmap");
if (bitmap != null) {
ivReceivedImage.setImageBitmap(bitmap);
tvImageInfo.setText(String.format("प्रकार: बिटमैप\nआकार: %dx%d px\nकॉन्फिग: %s",
bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig()));
} else {
tvImageInfo.setText("त्रुटि: बिटमैप नहीं मिला (संभवतः 1MB लिमिट से बड़ा)");
}
}
private void handleUri() {
String uriString = getIntent().getStringExtra("image_uri");
if (uriString != null) {
Uri uri = Uri.parse(uriString);
try {
ContentResolver resolver = getContentResolver();
InputStream inputStream = resolver.openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
if (inputStream != null) inputStream.close();
if (bitmap != null) {
ivReceivedImage.setImageBitmap(bitmap);
tvImageInfo.setText(String.format("प्रकार: कंटेंट URI\nआकार: %dx%d px\nURI: %s",
bitmap.getWidth(), bitmap.getHeight(), uriString));
} else {
tvImageInfo.setText("त्रुटि: URI से बिटमैप डिकोड नहीं कर सके");
}
} catch (Exception e) {
e.printStackTrace();
tvImageInfo.setText("त्रुटि: " + e.getMessage());
}
} else {
tvImageInfo.setText("त्रुटि: URI नहीं मिला");
}
}
}
तुलना: तीनों तरीके
| तरीका | पक्ष | विपक्ष | उपयोग का मामला |
|---|---|---|---|
| रिसोर्स ID (Int) | बहुत तेज, कोई मेमोरी कॉपी नहीं, कोई लिमिट नहीं | केवल ऐप के अपने रिसोर्सेज (R.drawable.*) | ऐप-इंटरनल इमेज, आइकन्स, प्लेसहोल्डर |
| बिटमैप (Parcelable) | कोई डिकोडिंग नहीं चाहिए, सीधा उपयोग | 1MB IPC लिमिट, मेमोरी भारी, OOM जोखिम | छोटी इमेज, थंबनेल, प्रोसेस्ड बिटमैप |
| कंटेंट URI (String) | कोई साइज लिमिट नहीं, गैलरी/कैमरा/फाइल्स के लिए काम करता है | रिसीवर में डिकोडिंग आवश्यक, अनुमति चाहिए | यूजर-चुनी इमेज, कैमरा फोटो, डाउनलोड |
एडवांस्ड: बड़ी इमेज के लिए FileProvider + URI
// FileProvider सेटअप (AndroidManifest.xml में)
<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>
// res/xml/file_paths.xml
<paths>
<files-path name="images" path="images/" />
<cache-path name="cache_images" path="cache_images/" />
</paths>
// फाइल से URI प्राप्त करें
File imageFile = new File(getFilesDir(), "images/photo.jpg");
Uri contentUri = FileProvider.getUriForFile(this, getPackageName() + ".fileprovider", imageFile);
// इंटेंट में पास करें
intent.putExtra("image_uri", contentUri.toString());
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // महत्वपूर्ण!
चरण 6: AndroidManifest.xml (दोनों एक्टिविटीज)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.app.sample">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!-- API 28 तक -->
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" /> <!-- API 33+ -->
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light">
<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>
<activity
android:name=".SecondActivity"
android:exported="false" />
</application>
</manifest>
महत्वपूर्ण: इंटेंट एक्स्ट्रा के माध्यम से बिटमैप पास करने की 1MB ट्रांजैक्शन बफर लिमिट होती है। बड़ी इमेज के लिए हमेशा कंटेंट URI (FileProvider/गैलरी) या रिसोर्स ID का उपयोग करें। बिटमैप पास करना केवल छोटी इमेज/थंबनेल के लिए उपयुक्त है।