Vous êtes sur la page 1sur 11

Displaying Bitmaps

Efficiently

facebook.com/apex.tgi

twitter.com/ApextgiNoida
pinterest.com/apextgi

Displaying Bitmaps Efficiently


It is a techniques to process and load Bitmapobjects in such a way that

keeps user interface (UI) components responsive and avoids exceeding


application memory limit.
If We are not careful, bitmaps can quickly consume your available memory

budget leading to an application crash due to the dreaded exception:


java.lang.OutofMemoryError: bitmap size exceeds VM budget.

Displaying Bitmaps Efficiently


Number of reasons why loading bitmaps in Android application
Mobile devices typically have constrained system resources. Android devices

can have as little as 16MB of memory available to a single application.


TheAndroid Compatibility Definition Document(CDD),Section 3.7. Virtual
Machine Compatibilitygives the required minimum application memory for
various screen sizes and densities. Applications should be optimized to perform
under this minimum memory limit. However, keep in mind many devices are
configured with higher limits.

Displaying Bitmaps Efficiently


Bitmaps take up a lot of memory, especially for rich images like

photographs. For example, the camera on theGalaxy Nexustakes photos up


to 2592x1936 pixels (5 megapixels). If the bitmap configuration used
isARGB_8888(the default from the Android 2.3 onward) then loading this
image into memory takes about 19MB of memory (2592*1936*4 bytes),
immediately exhausting the per-app limit on some devices.

Displaying Bitmaps Efficiently


Android app UIs frequently require several bitmaps to be loaded at once.

Components such asListView, GridViewandViewPagercommonly include


multiple bitmaps on-screen at once with many more potentially off-screen
ready to show at the flick of a finger.

Loading Large Bitmaps


Images come in all shapes and sizes. In some cases these are larger than required

for a given UI. For example, the system Gallery application displays photos taken
using Android device's camera which are typically much higher resolution than the
screen density of your device.
But we are working with limited memory, so only want to load a lower resolution

version in memory. The lower resolution version should match the size of the UI
component that displays it.

Loading Large Bitmaps


An image with a higher resolution does not provide any visible benefit, but still

takes up precious memory and incurs additional performance overhead due to


additional on the fly scaling.But We can also decoding large bitmaps without
exceeding the per application memory limit by loading a smaller sub sampled
version in memory.
TheBitmapFactoryclass provides several decoding methods such as

decodeByteArray(),decodeFile(), decodeResource(), etc. for creating


aBitmapfrom various sources.

Loading Large Bitmaps


Choose the most appropriate decode method based on image data source. These

methods attempt to allocate memory for the constructed bitmap and therefore can
easily result in anOutOfMemoryexception.

Each type of decode method has additional signatures that help to specify
decoding options via theBitmapFactory.Optionsclass.

Setting the inJustDecodeBoundsproperty totruewhile decoding avoids memory


allocation, returningnullfor the bitmap object but setting outWidth,outHeight and
outMimeType

Loading Large Bitmaps


Example:

BitmapFactory.Options options = new BitmapFactory.Options();


options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType

Contact

Thank You
Apex TG India
E-20 , Sector 63, Noida
0120
4029000/9024/9025/9027
Stay Connected with us for more PPT on Android

+91-9953584548
Email id:
pratap@apextgi.com

Apex TG India Pvt. Ltd. E-20 Sec-63 Noida