การเลือกรูปภาพจากแกลเลอรี่ (Image Gallery) ใน Android
ตัวอย่างนี้สาธิตวิธีเปิดแกลเลอรี่ให้ผู้ใช้เลือกรูปภาพ แล้วนำมาแสดงใน ImageView ผ่าน Intent.ACTION_PICK และ onActivityResult
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
เปิด Android Studio เลือก File → New Project และกรอกข้อมูลที่จำเป็น
ขั้นตอนที่ 2: จัดทำ Layout (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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_weight="0"
android:text="เลือกรูปจากแกลเลอรี่"
android:layout_gravity="center" />
</LinearLayout>
ขั้นตอนที่ 3: เขียนโค้ดหลัก (src/MainActivity.java)
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
ImageView imageView;
Button button;
private static final int PICK_IMAGE = 100;
Uri imageUri;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
button = findViewById(R.id.buttonLoadPicture);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openGallery();
}
});
}
private void openGallery() {
// เปิดแกลเลอรี่เลือกรูปภาพ
Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
imageUri = data.getData();
imageView.setImageURI(imageUri);
}
}
}
ขั้นตอนที่ 4: กำหนด Permission ใน AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<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>
การทดสอบ
รันแอปพลิเคชัน กดปุ่ม "เลือกรูปจากแกลเลอรี่" เลือกรูปภาพ จะแสดงใน ImageView

⚠️ ข้อควรระวังเรื่อง Permission (Android 10+ / API 29+)
- Scoped Storage: Android 10+ จำกัดการเข้าถึง External Storage ต้องใช้ MediaStore API
- READ_EXTERNAL_STORAGE: ยังต้องขอใน Manifest แต่ระบบจะจัดการให้ผ่าน MediaStore
- Android 13+ (API 33): ใช้
READ_MEDIA_IMAGESแทน READ_EXTERNAL_STORAGE
วิธีใหม่: Activity Result API (แนะนำสำหรับโปรเจกต์ใหม่)
// ใน Activity/Fragment
private ActivityResultLauncher<Intent> pickImageLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == RESULT_OK && result.getData() != null) {
Uri imageUri = result.getData().getData();
imageView.setImageURI(imageUri);
}
}
);
// เรียกใช้
private void openGallery() {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickImageLauncher.launch(intent);
}
การจัดการ Uri และ Bitmap
// แปลง Uri เป็น Bitmap (ถ้าต้องการประมวลผล)
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
// ทำอะไรกับ bitmap เช่น resize, compress, upload
} catch (IOException e) {
e.printStackTrace();
}