কম্পিউটার টিউটোরিয়াল

অ্যান্ড্রয়েডে কোটলিন ব্যবহার করে গ্যালারি থেকে ছবি বেছে নেওয়ার উপায়

ওভারভিউ

এই উদাহরণটি দেখায় কিভাবে অ্যান্ড্রয়েডে কোটলিন ব্যবহার করে গ্যালারি থেকে ছবি বেছে নিয়ে প্রদর্শন করা যায়।

ধাপ ১: নতুন প্রজেক্ট তৈরি করুন

Android Studio-তে File → New Project এ যান এবং প্রয়োজনীয় বিবরণ পূরণ করে একটি নতুন প্রজেক্ট তৈরি করুন।

ধাপ ২: লেআউট XML-এ ImageView এবং Button যোগ করুন

res/layout/activity_main.xml ফাইলে নিচের কোডটি যোগ করুন:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="2dp">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    <Button
        android:id="@+id/buttonLoadPicture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="0"
        android:text="ছবি লোড করুন" />
</LinearLayout>

ধাপ ৩: MainActivity.kt-এ লজিক লিখুন

src/MainActivity.kt ফাইলে নিচের কোডটি যোগ করুন:

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private lateinit var imageView: ImageView
    private lateinit var button: Button
    private val pickImage = 100
    private var imageUri: Uri? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
        imageView = findViewById(R.id.imageView)
        button = findViewById(R.id.buttonLoadPicture)

        button.setOnClickListener {
            val gallery = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI)
            startActivityForResult(gallery, pickImage)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK && requestCode == pickImage) {
            imageUri = data?.data
            imageView.setImageURI(imageUri)
        }
    }
}

ধাপ ৪: AndroidManifest.xml-এ READ_EXTERNAL_STORAGE পারমিশন যোগ করুন

androidManifest.xml ফাইলে নিচের কোডটি যোগ করুন:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.q11">
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <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>

অ্যাপ্লিকেশন চালান

আপনার অ্যান্ড্রয়েড ডিভাইস কম্পিউটারের সাথে সংযুক্ত থাকলে, Android Studio-তে Run আইকনে ক্লিক করে অ্যাপটি চালান। ডিভাইস নির্বাচন করুন এবং আউটপুট দেখুন।

  1. অ্যান্ড্রয়েডে URL থেকে ইমেজ ডাউনলোড করার পদ্ধতি

  2. অ্যান্ড্রয়েডে গ্যালারি থেকে ছবি পিক করার পদ্ধতি

  3. অ্যান্ড্রয়েডে কোড ব্যবহার করে ImageView-এর মার্জিন সেট করার পদ্ধতি

  4. Android-এ ImageView-এ ছবি ঘুরিয়ে দেওয়ার পদ্ধতি