ภาพรวม
ตัวอย่างนี้สาธิตวิธีเปิดแกลเลอรีภาพ (Gallery) ให้ผู้ใช้เลือกรูปภาพ แล้วนำมาแสดงใน ImageView โดยใช้ Intent.ACTION_PICK
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
สร้างโปรเจกต์ Android ใหม่ใน Android Studio
ขั้นตอนที่ 2: ออกแบบเลเอาต์ (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="2dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="centerCrop" />
<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="Load Picture" />
</LinearLayout>
ขั้นตอนที่ 3: เขียนโค้ดหลัก (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
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.result.registerForActivityResult
class MainActivity : AppCompatActivity() {
private lateinit var imageView: ImageView
private lateinit var button: Button
private var imageUri: Uri? = null
// ใช้ Activity Result API (แนะนำสำหรับ AndroidX)
private val pickImageLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
val data = result.data
imageUri = data?.data
data?.data?.let { uri ->
imageView.setImageURI(uri)
}
}
}
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 galleryIntent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
pickImageLauncher.launch(galleryIntent)
}
}
// วิธีเก่า (Deprecated) - เก็บไว้เพื่ออ้างอิง
/*
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK && requestCode == 100) {
imageUri = data?.data
imageView.setImageURI(imageUri)
}
}
*/
}
ขั้นตอนที่ 4: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.q11">
<!-- สำหรับ Android 10 (API 29) ต่ำกว่า -->
<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 10+ (API 29+)
- ไม่ต้องขอ
READ_EXTERNAL_STORAGEสำหรับการเข้าถึง MediaStore ผ่าน Intent - ระบบจะให้สิทธิ์ชั่วคราว (temporary URI permission) ให้กับ URI ที่ผู้ใช้เลือก
- ควรใช้ Activity Result API (
registerForActivityResult) แทนstartActivityForResultและonActivityResultที่ถูก Deprecated - สำหรับการเข้าถึงไฟล์โดยตรง (ไม่ผ่าน Picker) ให้ใช้
MediaStoreAPI หรือ Storage Access Framework