Giới thiệu
Hướng dẫn này trình bày cách mở thư viện ảnh (Gallery/Photos) để người dùng chọn một hình ảnh, sau đó hiển thị ảnh đó trong ImageView của ứng dụng.
Các bước thực hiện
Bước 1: Tạo dự án mới
Mở Android Studio, chọn File → New Project và điền thông tin cần thiết.
Bước 2: Thiết kế giao diện (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="Chọn ảnh từ thư viện"
android:layout_gravity="center" />
</LinearLayout>
Bước 3: Viết logic xử lý (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(v -> openGallery());
}
private void openGallery() {
// Mở thư viện ảnh hệ thống
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);
}
}
}
Bước 4: Cấu hình Manifest (androidManifest.xml) - Cần quyền
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<!-- Quyền đọc bộ nhớ ngoài (cần cho Android < 10) -->
<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>
Lưu ý quan trọng về quyền (Runtime Permissions)
- Android 10 (API 29)+: Không cần quyền READ_EXTERNAL_STORAGE để chọn ảnh qua ACTION_PICK (Scoped Storage)
- Android < 10: Cần xin quyền READ_EXTERNAL_STORAGE tại runtime
- Nên thêm
android:requestLegacyExternalStorage="true"trong application tag cho Android 10
Cách hiện đại (Activity Result API - khuyến nghị)
// Trong onCreate (Activity/Fragment)
ActivityResultLauncher<Intent> pickImageLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == RESULT_OK && result.getData() != null) {
Uri imageUri = result.getData().getData();
imageView.setImageURI(imageUri);
}
}
);
// Khi cần mở gallery
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickImageLauncher.launch(intent);
Chạy ứng dụng
Kết nối thiết bị Android, nhấn Run trong Android Studio. Nhấn "Chọn ảnh từ thư viện", chọn ảnh, và nó sẽ hiển thị trong ImageView.
