ภาพรวม
ตัวอย่างนี้สาธิตวิธีการแปลงภาพ (Bitmap) เป็นสตริง Base64 ใน Android เพื่อส่งผ่าน JSON/API หรือเก็บใน Database
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
เปิด Android Studio → File → New Project → กรอกรายละเอียดที่จำเป็น
ขั้นตอนที่ 2: จัดทำ Layout (res/layout/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"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:padding="4dp" />
</RelativeLayout>
ขั้นตอนที่ 3: เตรียมภาพใน Drawable
คัดลอกไฟล์ภาพ (เช่น logo.png) ไปวางใน res/drawable/
ขั้นตอนที่ 4: เขียนโค้ด MainActivity (src/MainActivity.java)
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.widget.TextView;
import java.io.ByteArrayOutputStream;
public class MainActivity extends AppCompatActivity {
TextView textView;
String string;
Bitmap bitmap;
@SuppressLint("WrongThread")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] bytes = byteArrayOutputStream.toByteArray();
// แปลง byte[] เป็น Base64 String
string = Base64.encodeToString(bytes, Base64.DEFAULT);
textView.setText(string);
}
}
ขั้นตอนที่ 5: กำหนดค่า AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<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>
Base64 Flags ที่ใช้งานได้
| Flag | คำอธิบาย |
|---|---|
Base64.DEFAULT |
มาตรฐาน มี line break ทุก 76 ตัวอักษร |
Base64.NO_WRAP |
ไม่มี line break (เหมาะสำหรับ JSON/API) |
Base64.URL_SAFE |
ใช้ - และ _ แทน + และ / (ปลอดภัยสำหรับ URL) |
การแปลงกลับ (Base64 → Bitmap)
// Decode Base64 กลับเป็น Bitmap byte[] decodedBytes = Base64.decode(base64String, Base64.DEFAULT); Bitmap decodedBitmap = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length); imageView.setImageBitmap(decodedBitmap);
ข้อควรระวัง
- Base64 String จะใหญ่กว่า Binary ประมาณ 33%
- ไม่ควรเก็บภาพขนาดใหญ่เป็น Base64 ใน Database (ช้า หน่วยความจำมาก)
- ใช้
Base64.NO_WRAPสำหรับ JSON API - พิจารณา Resize/Compress ภาพก่อน Encode เพื่อลดขนาด
การทดสอบ
รันแอปบนอุปกรณ์จริง จะแสดงสตริง Base64 ยาวๆ ของภาพ logo
