1page.title=Displaying Bitmaps Efficiently 2page.tags="bitmaps","images","graphics" 3 4trainingnavtop=true 5startpage=true 6 7@jd:body 8 9<div id="tb-wrapper"> 10<div id="tb"> 11 12<h2>Dependencies and prerequisites</h2> 13<ul> 14 <li>Android 2.1 (API Level 7) or higher</li> 15 <li><a href="{@docRoot}tools/support-library/index.html">Support Library</a></li> 16</ul> 17 18<h2>Try it out</h2> 19 20<div class="download-box"> 21 <a href="{@docRoot}shareables/training/BitmapFun.zip" class="button">Download the sample</a> 22 <p class="filename">BitmapFun.zip</p> 23</div> 24 25</div> 26</div> 27 28<p>Learn how to use common techniques to process and load {@link 29android.graphics.Bitmap} objects in a way that keeps your user interface (UI) components responsive 30and avoids exceeding your application memory limit. If you're not careful, bitmaps can quickly 31consume your available memory budget leading to an application crash due to the dreaded 32exception:<br />{@code java.lang.OutofMemoryError: bitmap size exceeds VM budget}.</p> 33 34<p>There are a number of reasons why loading bitmaps in your Android application is tricky:</p> 35 36<ul> 37 <li>Mobile devices typically have constrained system resources. Android devices can have as little 38 as 16MB of memory available to a single application. The <a 39 href="http://source.android.com/compatibility/downloads.html">Android Compatibility Definition 40 Document</a> (CDD), <i>Section 3.7. Virtual Machine Compatibility</i> gives the required minimum 41 application memory for various screen sizes and densities. Applications should be optimized to 42 perform under this minimum memory limit. However, keep in mind many devices are configured with 43 higher limits.</li> 44 <li>Bitmaps take up a lot of memory, especially for rich images like photographs. For example, the 45 camera on the <a href="http://www.android.com/devices/detail/galaxy-nexus">Galaxy Nexus</a> takes 46 photos up to 2592x1936 pixels (5 megapixels). If the bitmap configuration used is {@link 47 android.graphics.Bitmap.Config ARGB_8888} (the default from the Android 2.3 onward) then loading 48 this image into memory takes about 19MB of memory (2592*1936*4 bytes), immediately exhausting the 49 per-app limit on some devices.</li> 50 <li>Android app UI’s frequently require several bitmaps to be loaded at once. Components such as 51 {@link android.widget.ListView}, {@link android.widget.GridView} and {@link 52 android.support.v4.view.ViewPager} commonly include multiple bitmaps on-screen at once with many 53 more potentially off-screen ready to show at the flick of a finger.</li> 54</ul> 55 56<h2>Lessons</h2> 57 58<dl> 59 <dt><b><a href="load-bitmap.html">Loading Large Bitmaps Efficiently</a></b></dt> 60 <dd>This lesson walks you through decoding large bitmaps without exceeding the per application 61 memory limit.</dd> 62 63 <dt><b><a href="process-bitmap.html">Processing Bitmaps Off the UI Thread</a></b></dt> 64 <dd>Bitmap processing (resizing, downloading from a remote source, etc.) should never take place 65 on the main UI thread. This lesson walks you through processing bitmaps in a background thread 66 using {@link android.os.AsyncTask} and explains how to handle concurrency issues.</dd> 67 68 <dt><b><a href="cache-bitmap.html">Caching Bitmaps</a></b></dt> 69 <dd>This lesson walks you through using a memory and disk bitmap cache to improve the 70 responsiveness and fluidity of your UI when loading multiple bitmaps.</dd> 71 72 <dt><b><a href="manage-memory.html">Managing Bitmap Memory</a></b></dt> 73 <dd>This lesson explains how to manage bitmap memory to maximize your app's performance.</dd> 74 75 <dt><b><a href="display-bitmap.html">Displaying Bitmaps in Your UI</a></b></dt> 76 <dd>This lesson brings everything together, showing you how to load multiple bitmaps into 77 components like {@link android.support.v4.view.ViewPager} and {@link android.widget.GridView} 78 using a background thread and bitmap cache.</dd> 79 80</dl> 81