Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีส่งรูปภาพระหว่าง Activity ใน Android

ภาพรวม

ตัวอย่างนี้แสดงวิธีการส่งรูปภาพจาก Activity หนึ่งไปอีก Activity หนึ่ง โดยส่ง Resource ID (Integer) ผ่าน Intent แทนการส่ง Bitmap โดยตรง (ประหยัดหน่วยความจำ)

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio ไปที่ File → New Project แล้วกรอกข้อมูลที่จำเป็น

ขั้นตอนที่ 2: จัดรูปแบบหน้าจอ Activity หลัก (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/btnSend"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:onClick="SendImage"
        android:text="Send Image" />
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/btnSend"
        android:layout_marginTop="10dp"
        android:src="@drawable/image" />
</RelativeLayout>

ขั้นตอนที่ 3: เขียนโค้ด Activity หลัก (MainActivity.java)

package com.example.imagepassing;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void SendImage(View view) {
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        // ส่ง Resource ID (Integer) แทน Bitmap
        intent.putExtra("resId", R.drawable.image);
        startActivity(intent);
    }
}

ขั้นตอนที่ 4: สร้าง Activity ที่สอง

activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp"
    tools:context=".SecondActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="Second Activity"
        android:textSize="24sp"
        android:textStyle="bold" />
    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/textView"
        android:layout_marginTop="5dp" />
</RelativeLayout>

SecondActivity.java

package com.example.imagepassing;

import android.os.Bundle;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;

public class SecondActivity extends AppCompatActivity {
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        
        imageView = findViewById(R.id.imageView2);
        
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            int resId = bundle.getInt("resId", 0);
            if (resId != 0) {
                imageView.setImageResource(resId);
            }
        }
    }
}

ขั้นตอนที่ 5: ตั้งค่า Manifest

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

การทดสอบ

รันแอปพลิเคชันบนอุปกรณ์จริง กดปุ่ม "Send Image" จะเปิด Second Activity แสดงรูปภาพเดียวกัน

วิธีส่งรูปภาพระหว่าง Activity ใน Android

วิธีอื่นๆ และ Best Practices

1. ส่ง Bitmap (ไม่แนะนำสำหรับรูปใหญ่)

// Sender
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
intent.putExtra("bitmap", bitmap); // ต้อง Parcelable

// Receiver
Bitmap bitmap = getIntent().getParcelableExtra("bitmap");
imageView.setImageBitmap(bitmap);

2. ส่ง Uri (แนะนำสำหรับรูปจาก Gallery/Camera)

// Sender
intent.putExtra("imageUri", imageUri.toString());

// Receiver
String uriString = getIntent().getStringExtra("imageUri");
imageView.setImageURI(Uri.parse(uriString));

3. ใช้ Glide/Picasso โหลดจาก URL/File

// ส่ง URL หรือ File Path แล้วให้ Library โหลดใน Activity ถัดไป
Glide.with(this).load(urlOrPath).into(imageView);

สรุป

  • Resource ID: เร็วที่สุด ประหยัด RAM เหมาะสำหรับรูปใน res/drawable
  • Uri/String Path: เหมาะสำหรับไฟล์จริง (Gallery, Camera, Download)
  • Bitmap: ใช้เฉพาะรูปเล็กมาก (Icon) ไม่เช่นนั้นจะทำให้ TransactionTooLargeException