ওভারভিউ
এই টিউটোরিয়ালটি দেখাবে কিভাবে একটি অ্যান্ড্রয়েড অ্যাপ্লিকেশনে একটি ইমেজ (ড্রয়েবল রিসোর্স) এক এক্টিভিটি থেকে অন্য এক্টিভিটিতে পাস করা যায়। আমরা Intent এর putExtra() মেথড ব্যবহার করে ড্রয়েবল রিসোর্স আইডি পাস করব এবং দ্বিতীয় এক্টিভিটিতে তা রিসিভ করে ImageView-এ দেখাব।
পূর্বশর্ত
- অ্যান্ড্রয়েড স্টুডিও ইনস্টল করা থাকতে হবে
- বেসিক জাভা ও অ্যান্ড্রয়েড কম্পোনেন্ট জ্ঞান
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
অ্যান্ড্রয়েড স্টুডিও খুলুন → File → New Project → Empty Activity সিলেক্ট করুন → প্রজেক্টের নাম, প্যাকেজ নাম, ল্যাঙ্গুয়েজ (Java) নির্বাচন করুন → Finish ক্লিক করুন।
ধাপ ২: মেইন লেআউট ডিজাইন করুন (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">
<Button
android:id="@+id/btnSend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:onClick="sendImage"
android:text="Send Image" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/btnSend"
android:layout_marginTop="10dp"
android:src="@drawable/your_image" />
</RelativeLayout>
নোট: @drawable/your_image আপনার প্রজেক্টের res/drawable ফোল্ডারে থাকা কোনো ইমেজের নাম দিয়ে প্রতিস্থাপন করুন।
ধাপ ৩: MainActivity.java লিখুন
package com.example.imagepass;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendImage(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("resId", R.drawable.your_image); // ড্রয়েবল রিসোর্স আইডি পাস করুন
startActivity(intent);
}
}
ধাপ ৪: দ্বিতীয় এক্টিভিটি তৈরি করুন
লেআউট: 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="8dp"
tools:context=".SecondActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="Second Activity"
android:textSize="24sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/textView"
android:layout_marginTop="5dp" />
</RelativeLayout>
জাভা কোড: SecondActivity.java
package com.example.imagepass;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class SecondActivity extends AppCompatActivity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
imageView = findViewById(R.id.imageView2);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
int resId = bundle.getInt("resId");
imageView.setImageResource(resId);
}
}
}
ধাপ ৫: AndroidManifest.xml এ দ্বিতীয় এক্টিভিটি ডিক্লেয়ার করুন
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.imagepass">
<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/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity" />
</application>
</manifest>
চলান ও পরীক্ষা করুন
ডিভাইস বা এমুলেটর কানেক্ট করে Run বাটনে ক্লিক করুন। প্রথম স্ক্রিনে "Send Image" বাটনে ট্যাপ করলে দ্বিতীয় এক্টিভিটি খুলবে এবং ইমেজ প্রদর্শিত হবে।
উন্নত টিপস
- বড় ইমেজের জন্য
Bitmapপাস করলে মেমরি সমস্যা হতে পারে। সেক্ষেত্রে ফাইল পাথ বা URI পাস করা ভালো। ParcelableবাSerializableব্যবহার করে জটিল অবজেক্ট পাস করা যায়।- আধুনিক অ্যান্ড্রয়েড ডেভেলপমেন্টে
Activity Result APIব্যবহার করার পরামর্শ দেওয়া হয়।