ওভারভিউ
এই টিউটোরিয়ালটি দেখায় কিভাবে Intent.ACTION_SEND ব্যবহার করে ফাইল আট্যাচমেন্ট (ছবি/ডকুমেন্ট) সহ ইমেইল কম্পোজ উইন্ডো খোলা যায়। ব্যবহারকারী ইমেইল ক্লায়েন্ট (Gmail, Outlook ইত্যাদি) বেছে পাঠাতে পারবে।
পূর্বশর্ত
- অ্যান্ড্রয়েড স্টুডিও (কোটলিন)
- READ_EXTERNAL_STORAGE পারমিশন (Android 9-এ)
- ইন্টারনেট পারমিশন
ধাপ ১: প্রজেক্ট তৈরি করুন
File → New Project → Empty Activity → Language: Kotlin।
ধাপ ২: লেআউট (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="4dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/etTo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:hint="Receiver's Email Address!"
android:inputType="textEmailAddress"
android:singleLine="true" />
<EditText
android:id="@+id/etSubject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:hint="Enter Subject"
android:singleLine="true" />
<EditText
android:id="@+id/etMessage"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="5dp"
android:gravity="top|start"
android:hint="Compose Email"
android:inputType="textMultiLine" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btSend"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:text="Send" />
<Button
android:id="@+id/btAttachment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="Attachment" />
</RelativeLayout>
<TextView
android:id="@+id/tvAttachment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableStart="@drawable/ic_baseline_attach_file_24"
android:visibility="gone" />
</LinearLayout>
ধাপ ৩: MainActivity.kt
package com.example.emailattachment
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
lateinit var etEmail: EditText
lateinit var etSubject: EditText
lateinit var etMessage: EditText
lateinit var send: Button
lateinit var attachment: Button
lateinit var tvAttachment: TextView
lateinit var email: String
lateinit var subject: String
lateinit var message: String
lateinit var uri: Uri
private val PICK_FROM_GALLERY = 101
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
etEmail = findViewById(R.id.etTo)
etSubject = findViewById(R.id.etSubject)
etMessage = findViewById(R.id.etMessage)
attachment = findViewById(R.id.btAttachment)
tvAttachment = findViewById(R.id.tvAttachment)
send = findViewById(R.id.btSend)
send.setOnClickListener { sendEmail() }
attachment.setOnClickListener { openFolder() }
}
private fun openFolder() {
val intent = Intent().apply {
type = "image/*" // অথবা "*/*" সব ফাইলের জন্য
action = Intent.ACTION_GET_CONTENT
putExtra("return-data", true)
}
startActivityForResult(
Intent.createChooser(intent, "Complete action using"),
PICK_FROM_GALLERY
)
}
private fun sendEmail() {
try {
email = etEmail.text.toString()
subject = etSubject.text.toString()
message = etMessage.text.toString()
val emailIntent = Intent(Intent.ACTION_SEND).apply {
type = "message/rfc822" // ইমেইল ক্লায়েন্ট ফিল্টার
putExtra(Intent.EXTRA_EMAIL, arrayOf(email))
putExtra(Intent.EXTRA_SUBJECT, subject)
putExtra(Intent.EXTRA_TEXT, message)
if (::uri.isInitialized) putExtra(Intent.EXTRA_STREAM, uri)
}
startActivity(Intent.createChooser(emailIntent, "Sending email..."))
} catch (t: Throwable) {
Toast.makeText(this, "Request failed try again: $t", Toast.LENGTH_LONG).show()
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == PICK_FROM_GALLERY && resultCode == RESULT_OK && data != null) {
uri = data.data!!
tvAttachment.text = uri.lastPathSegment ?: "Attachment"
tvAttachment.visibility = View.VISIBLE
}
}
}
ধাপ ৪: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.emailattachment">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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>
</application>
</manifest>
চলান ও পরীক্ষা
- "Attachment" বাটনে ক্লিক করে গ্যালারি থেকে ছবি বেছে নিন।
- ইমেইল, সাবজেক্ট, মেসেজ লিখুন।
- "Send" বাটনে ক্লিক করলে ইমেইল ক্লায়েন্ট চয়েনার খুলবে।
- Gmail/অন্যান্য অ্যাপ বেছে পাঠান।
গুরুত্বপূর্ণ নোট (Android 10+ / Scoped Storage)
ACTION_GET_CONTENTসিস্টেম পিকার ব্যবহার করে, তাই পারমিশন ছাড়াই কাজ করে।READ_EXTERNAL_STORAGEশুধু Android 9 (API 28) এবং নিচের জন্য লাগে।- আধুনিক অ্যাপে
ActivityResultContracts.GetContent()ব্যবহার করুন (Activity Result API)।
একাধিক আট্যাচমেন্ট
val uris: ArrayList= arrayListOf(uri1, uri2, uri3) emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris) emailIntent.type = "*/*" // মাল্টিপল টাইপের জন্য
সতর্কতা
- ইমেইল অ্যাপ ইনস্টল না থাকলে ক্র্যাশ করতে পারে -
resolveActivity()চেক করুন। - বড় ফাইল (>25MB) ইমেইল সার্ভার রিজেক্ট করতে পারে।
- URI পারমিশন:
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)যোগ করুন।