Android Bitmap memory usage is too high

Source: Internet
Author: User

Sometimes you may find errors due to insufficient memory, mostly because the Image is too large. The following is a simple and effective method:

BitmapFactory. Options opts = new BitmapFactory. Options ();
Opts. inSampleSize = 4;
Bitmap bitmap = BitmapFactory. decodeFile (imageFile, opts );
Setting the appropriate inSampleSize is one of the keys to solve this problem. BitmapFactory. Options provides another member, inJustDecodeBounds.
BitmapFactory. Options opts = new BitmapFactory. Options (); opts. inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory. decodeFile (imageFile, opts );
After inJustDecodeBounds is set to true, decodeFile does not allocate space, but the length and width of the original image can be calculated, that is, opts. width and opts. height.
With these two parameters, you can use a certain algorithm to obtain an appropriate inSampleSize.
Android provides a dynamic computing method to view the Android source code.
Public static int computeSampleSize (BitmapFactory. Options options, int minSideLength, int maxNumOfPixels ){
Int initialSize = computeInitialSampleSize (options, minSideLength, maxNumOfPixels );
Int roundedSize;
If (initialSize <= {
RoundedSize = 1;
While (roundedSize <initialSize ){
RoundedSize <= 1;
}
} Else {
RoundedSize = (initialSize + 7)/8*8;
}
Return roundedSize ;}
Private static int computeInitialSampleSize (BitmapFactory. Options options, int minSideLength, int maxNumOfPixels ){
Double w = options. outWidth;
Double h = options. outHeight;

 

 

 

Android bitmap memory limit OOM, out of memory.

 

Here, I use Gallery as an example. In the simulator, there will be no OOM error. However, once the program is run into the real machine, there will be more than one picture file, OOM will inevitably appear, we can avoid this by doing some additional processing.

1. create an image cache object HashMap <Integer, Bitmap> dataCache. integer corresponds to the position in the Adapter. We only use the cached image in the display. If there is a corresponding image in dataCache, we need to recycle the memory. In this example, the getView method of the Adapter object first checks whether the location has cached bitmap. If not, the image (bitmapDecoder. getPhotoItem, BitmapDecoder class) and return the bitmap object. Set the bitmap cache of dataCache in this location for later use. If there is a cache in this location, it will be used directly, this avoids the memory overhead required to call the underlying decoding image again. Sometimes, in order to improve the update speed of Gallery, we can pre-store bitmap in some locations, such as storing bitmap in three down three positions outside the display area, in this way, getView can be obtained quickly when Gallery is rolled up or down.

[Java] public View getView (int position, View convertView, ViewGroup parent ){

If (convertView = null ){
LayoutInflater inflater = LayoutInflater. from (context );
ConvertView = inflater. inflate (R. layout. photo_item, null );
 
Holder = new ViewHolder ();
Holder. photo = (ImageView) convertView. findViewById (R. id. photo_item_image );
Holder. photoTitle = (TextView) convertView. findViewById (R. id. photo_item_title );
Holder. photoDate = (TextView) convertView. findViewById (R. id. photo_item_date );
ConvertView. setTag (holder );
} Else {
Holder = (ViewHolder) convertView. getTag ();
}
Cursor. moveToPosition (position );

Bitmap current = dateCache. get (position );
If (current! = Null) {// if the image has been decoded in the cache, the cached image is returned directly.
Holder. photo. setImageBitmap (current );
} Else {
Current = bitmapDecoder. getPhotoItem (cursor. getString (1), 2 );
Holder. photo. setImageBitmap (current );
DateCache. put (position, current );
}
Holder. photoTitle. setText (cursor. getString (2 ));
Holder. photoDate. setText (cursor. getString (4 ));
Return convertView;
}
 
Public View getView (int position, View convertView, ViewGroup parent ){

If (convertView = null ){
LayoutInflater inflater = LayoutInflater. from (context );
ConvertView = inflater. inflate (R. layout. photo_item, null );

Holder = new ViewHolder ();
Holder. photo = (ImageView) convertView. findViewById (R. id. photo_item_image );
Holder. photoTitle = (TextView) convertView. findViewById (R. id. photo_item_title );
Holder. photoDate = (TextView) convertView. findViewById (R. id. photo_item_date );
ConvertView. setTag (holder );
} Else {
Holder = (ViewHolder) convertView. getTag ();
}
Cursor. moveToPosition (position );

Bitmap current = dateCache. get (position );
If (current! = Null) {// if the image has been decoded in the cache, the cached image is returned directly.
Holder. photo. setImageBitmap (current );
} Else {
Current = bitmapDecoder. getPhotoItem (cursor. getString (1), 2 );
Holder. photo. setImageBitmap (current );
DateCache. put (position, current );
}
Holder. photoTitle. setText (cursor. getString (2 ));
Holder. photoDate. setText (cursor. getString (4 ));
Return convertView;
}

}
 
BitmapDecoder. class


[Java] package com. wuyi. bestjoy;
 
Import java. io. FileNotFoundException;
Import java. io. FileOutputStream;
 
Import android. content. Context;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android. graphics. Matrix;
 
Public class BitmapDecoder {
Private static final String TAG = "BitmapDecoder ";
Private Context context;
Public BitmapDecoder (Context context ){
This. context = context;
}

Public Bitmap getPhotoItem (String filepath, int size ){
BitmapFactory. Options options = new BitmapFactory. Options ();
Options. inSampleSize = size;
Bitmap bitmap = BitmapFactory. decodeFile (filepath, options );
Bitmap = Bitmap. createScaledBitmap (bitmap, 180,251, true); // pre-scale to avoid real-time scaling and improve the update rate
Return bitmap;

}
}
Package com. wuyi. bestjoy;

Import java. io. FileNotFoundException;
Import java. io. FileOutputStream;

Import android. content. Context;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android. graphics. Matrix;

Public class BitmapDecoder {
Private static final String TAG = "BitmapDecoder ";
Private Context context;
Public BitmapDecoder (Context context ){
This. context = context;
}
 
Public Bitmap getPhotoItem (String filepath, int size ){
BitmapFactory. Options options = new BitmapFactory. Options ();
Options. inSampleSize = size;
Bitmap bitmap = BitmapFactory. decodeFile (filepath, options );
Bitmap = Bitmap. createScaledBitmap (bitmap, 180,251, true); // pre-scale to avoid real-time scaling and improve the update rate
Return bitmap;

}
}
 
 

2. Due to the features of the Gallery control, there is always an item in the selected state. We can use this to clear unnecessary bitmap in dataCache to release the memory.


[Java] @ Override
Public void onItemSelected (AdapterView <?> Parent, View view, int position, long id ){

ReleaseBitmap ();
Log. v (TAG, "select id:" + id );
}
 
Private void releaseBitmap (){
// Here, We pre-store bitmap at three locations out of the first and last visible locations.
// That is, only M bitmaps (M = 6 + number of currently visible views of Gallery) are cached in dataCache.
Int start = mGallery. getFirstVisiblePosition ()-3;
Int end = mGallery. getLastVisiblePosition () + 3;
Log. v (TAG, "start:" + start );
Log. v (TAG, "end:" + end );
// Release bitmap resources outside position <start
Bitmap delBitmap;
For (int del = 0; del <start; del ++ ){
DelBitmap = dateCache. get (del );
If (delBitmap! = Null ){
// If it is not empty, it indicates a cached bitmap and needs to be cleared.
Log. v (TAG, "release position:" + del );
// Remove the del-> bitmap ing from the cache
DateCache. remove (del );
DelBitmap. recycle ();
}
}
 
FreeBitmapFromIndex (end );

}

/**
* Release bitmap resources from a location
* @ Param index
*/
Private void freeBitmapFromIndex (int end ){
// Release other bitmap Resources
Bitmap delBitmap;
For (int del = end + 1; del <dateCache. size (); del ++ ){
DelBitmap = dateCache. get (del );
If (delBitmap! = Null ){
DateCache. remove (del );
DelBitmap. recycle ();
Log. v (TAG, "release position:" + del );
}

}
}
@ Override
Public void onItemSelected (AdapterView <?> Parent, View view, int position, long id ){

ReleaseBitmap ();
Log. v (TAG, "select id:" + id );
}

Private void releaseBitmap (){
// Here, We pre-store bitmap at three locations out of the first and last visible locations.
// That is, only M bitmaps (M = 6 + number of currently visible views of Gallery) are cached in dataCache.
Int start = mGallery. getFirstVisiblePosition ()-3;
Int end = mGallery. getLastVisiblePosition () + 3;
Log. v (TAG, "start:" + start );
Log. v (TAG, "end:" + end );
// Release bitmap resources outside position <start
Bitmap delBitmap;
For (int del = 0; del <start; del ++ ){
DelBitmap = dateCache. get (del );
If (delBitmap! = Null ){
// If it is not empty, it indicates a cached bitmap and needs to be cleared.
Log. v (TAG, "release position:" + del );
// Remove the del-> bitmap ing from the cache
DateCache. remove (del );
DelBitmap. recycle ();
}
}

FreeBitmapFromIndex (end );

}
 
/**
* Release bitmap resources from a location
* @ Param index
*/
Private void freeBitmapFromIndex (int end ){
// Release other bitmap Resources
Bitmap delBitmap;
For (int del = end + 1; del <dateCache. size (); del ++ ){
DelBitmap = dateCache. get (del );
If (delBitmap! = Null ){
DateCache. remove (del );
DelBitmap. recycle ();
Log. v (TAG, "release position:" + del );
}

}
}

These additional operations effectively prevent OOM problems.

 


From Turing's dream

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.