การจัดการบิตแมป
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ลองใช้ Compose
Jetpack Compose เป็นชุดเครื่องมือ UI ที่แนะนำสำหรับ Android ดูวิธีแสดงกราฟิกในฟีเจอร์เขียน
การโหลดบิตแมปในแอป Android อาจเป็นเรื่องยากด้วยเหตุผลหลายประการ ดังนี้
- บิตแมปอาจทำให้งบประมาณหน่วยความจำของแอปหมดลงได้ง่ายๆ เช่น กล้องในโทรศัพท์ Pixel จะถ่ายภาพได้สูงสุด 4048x3036 พิกเซล (12 เมกะพิกเซล)
หากการกำหนดค่าบิตแมปที่ใช้คือ
ARGB_8888
ซึ่งเป็นค่าเริ่มต้นสำหรับ Android 2.3 (API ระดับ 9) ขึ้นไป การโหลดรูปภาพเดียวลงในหน่วยความจำจะใช้หน่วยความจำประมาณ 48 MB (4048*3036*4 ไบต์) ความต้องการหน่วยความจำที่มากเช่นนี้อาจทำให้หน่วยความจำทั้งหมดที่แอปใช้ได้หมดลงในทันที
- การโหลดบิตแมปในเทรด UI อาจทำให้ประสิทธิภาพของแอปแย่ลง ซึ่งส่งผลให้
การตอบสนองช้าหรือแม้แต่ข้อความ ANR ดังนั้นจึงควรจัดการการแยกเธรด
อย่างเหมาะสมเมื่อทำงานกับบิตแมป
- หากแอปโหลดบิตแมปหลายรายการลงในหน่วยความจำ คุณจะต้องจัดการ
การแคชหน่วยความจำและดิสก์อย่างชำนาญ ไม่เช่นนั้น ความตอบสนองและความลื่นไหลของ UI ของแอป
อาจได้รับผลกระทบ
ในกรณีส่วนใหญ่ เราขอแนะนำให้คุณใช้ไลบรารี Glide
เพื่อดึงข้อมูล ถอดรหัส และแสดงบิตแมปในแอปของคุณ Glide จะซ่อนความซับซ้อนส่วนใหญ่ในการจัดการงานเหล่านี้และ
งานอื่นๆ ที่เกี่ยวข้องกับการทำงานกับบิตแมปและรูปภาพอื่นๆ ใน Android
ดูข้อมูลเกี่ยวกับการใช้และการดาวน์โหลด Glide ได้ที่ที่เก็บ Glide ใน GitHub
นอกจากนี้ คุณยังเลือกใช้ API ระดับล่างที่สร้างขึ้นในเฟรมเวิร์ก Android โดยตรงได้ด้วย
ดูข้อมูลเพิ่มเติมเกี่ยวกับวิธีดำเนินการดังกล่าวได้ที่
การโหลดบิตแมปขนาดใหญ่อย่างมีประสิทธิภาพ
การแคชบิตแมป และ
การจัดการหน่วยความจำบิตแมป
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[null,null,["อัปเดตล่าสุด 2025-07-27 UTC"],[],[],null,["# Handling bitmaps\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to display graphics in Compose. \n[ImageBitmap →](/jetpack/compose/graphics/images/compare) \n\nThere are a number of reasons why loading bitmaps in your Android app is tricky:\n\n- Bitmaps can very easily exhaust an app's memory budget. For example, the camera on the [Pixel](https://www.android.com/phones/pixel/) phone takes photos of up to 4048x3036 pixels (12 megapixels). If the bitmap configuration used is [ARGB_8888](/reference/android/graphics/Bitmap.Config), the default for Android 2.3 (API level 9) and higher, loading a single photo into memory takes about 48MB of memory (4048\\*3036\\*4 bytes). Such a large memory demand can immediately use up all the memory available to the app.\n- Loading bitmaps on the UI thread can degrade your app's performance, causing slow responsiveness or even ANR messages. It is therefore important to manage threading appropriately when working with bitmaps.\n- If your app is loading multiple bitmaps into memory, you need to skillfully manage memory and disk caching. Otherwise, the responsiveness and fluidity of your app's UI may suffer.\n\n\nFor most cases, we recommend that you use the [Glide](https://github.com/bumptech/glide)\nlibrary to fetch, decode, and display bitmaps in your app. Glide abstracts out most of\nthe complexity in handling these and\nother tasks related to working with bitmaps and other images on Android.\nFor information about using and downloading Glide, visit the\n[Glide repository](https://github.com/bumptech/glide) on GitHub.\n\n\nYou can also opt to work directly with the lower-level APIs built into the Android framework.\nFor more information on doing so, refer to\n[Loading Large Bitmaps Efficiently](/topic/performance/graphics/load-bitmap),\n[Caching Bitmaps](/topic/performance/graphics/cache-bitmap), and\n[Managing Bitmap Memory](/topic/performance/graphics/manage-memory).\n\nMore resources\n--------------"]]