Out of Memory Exception Due to Large Bitmap Size

outofmemory exception for Large Bitmap

MediaStore.getBitmap is just a simple convienence method, it looks like this:

public static final Bitmap getBitmap(ContentResolver cr, Uri url)
throws FileNotFoundException, IOException {
InputStream input = cr.openInputStream(url);
Bitmap bitmap = BitmapFactory.decodeStream(input);
input.close();
return bitmap;
}

You can create your own method based on this that takes the options and calls a different overload on BitmapFactory:

public static final Bitmap getBitmap(ContentResolver cr,
Uri url,
BitmapFactory.Options options)
throws FileNotFoundException, IOException {
InputStream input = cr.openInputStream(url);
Bitmap bitmap = BitmapFactory.decodeStream(input, null, options);
input.close();
return bitmap;
}

Usage:

final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;

Bitmap bm = getBitmap(getActivity().getContentResolver(),
Uri.parse(selectedImageToUri),
options);

Out of memory exception due to large bitmap size

Its not always a good idea to store bitmaps in memory. If you really want to do so then try using SoftReference for your map. Check this

make your map's value argument as SoftReference<Bitmap>. Then while searching in the map use this code snippet

@SuppressWarnings("unchecked")
public class DrawableManager {

@SuppressWarnings("rawtypes")
private final Map<String, SoftReference<Bitmap>> drawableMap;

@SuppressWarnings("rawtypes")
private DrawableManager() {
drawableMap = new HashMap<String, SoftReference<Bitmap>>();
}

static private DrawableManager _instance;

static public DrawableManager getInstance() {
if(_instance == null) {
_instance = new DrawableManager();
}
return _instance;
}

public Bitmap fetchBitmap(final String sURL) {
if(sURL.length() == 0)
return null;
Bitmap bm = null;
SoftReference<Bitmap> reference = drawbaleM.get(imagePath);
if(reference != null) bm = reference.get();
if(bm != null) {
return bm;
}

byte[] imageData = ThumbImg(sURL);

if(imageData == null)
return null;

if(imageData.length > 0) {
bm = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
if(bm != null) {
drawableMap.put(sURL, bm);
}
return bm;
}
else {
return null;
}
}

public void fetchBitmapOnThread(final String sURL, final ImageView imageView) {
if (drawableMap.containsKey(sURL)) {
imageView.setImageBitmap((Bitmap) drawableMap.get(sURL));
}

final Handler handler = new Handler() {
@Override
public void handleMessage(Message message) {
imageView.setImageBitmap((Bitmap) message.obj);
}
};

Thread thread = new Thread() {
@Override
public void run() {
Bitmap bitmap = fetchBitmap(sURL);
Message message = handler.obtainMessage(1, bitmap);
handler.sendMessage(message);
}
};
thread.start();
}

@SuppressWarnings("unused")
public static byte[] ThumbImg(String imgUrl)
{

//first check in the cache, if not available then store in the sd card memory
HttpURLConnection connection = null;
String userAgent = null;

try
{
URL url = new URL(imgUrl);
connection = ( HttpURLConnection ) url.openConnection();
if(userAgent != null) {
connection.setRequestProperty("User-Agent", userAgent);
}
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int CHUNKSIZE = 8192; //size of fixed chunks
int BUFFERSIZE = 1024; //size of reading buffer

int bytesRead = 0;
byte[] buffer = new byte[BUFFERSIZE]; //initialize buffer
byte[] fixedChunk = new byte[CHUNKSIZE]; //initialize 1st chunk
ArrayList<byte[]> BufferChunkList = new ArrayList<byte[]>(); // List of chunk data
int spaceLeft = CHUNKSIZE;
int chunkIndex = 0;

DataInputStream in = new DataInputStream(connection.getInputStream() );

while( ( bytesRead = in.read( buffer ) ) != -1 ) { //loop until the DataInputStream is completed
if(bytesRead > spaceLeft) {
//copy to end of current chunk
System.arraycopy(buffer, 0, fixedChunk, chunkIndex, spaceLeft);
BufferChunkList.add(fixedChunk);

//create a new chunk, and fill in the leftover
fixedChunk = new byte[CHUNKSIZE];
chunkIndex = bytesRead - spaceLeft;
System.arraycopy(buffer, spaceLeft, fixedChunk, 0, chunkIndex);
} else {
//plenty of space, just copy it in
System.arraycopy(buffer, 0, fixedChunk, chunkIndex, bytesRead);
chunkIndex = chunkIndex + bytesRead;
}
spaceLeft = CHUNKSIZE - chunkIndex;
}

if (in != null) {
in.close();
}

// copy it all into one big array
int responseSize = (BufferChunkList.size() * CHUNKSIZE) + chunkIndex;
Log.d("response size",""+responseSize);
byte[] responseBody = new byte[responseSize];
int index = 0;
for(byte[] b : BufferChunkList) {
System.arraycopy(b, 0, responseBody, index, CHUNKSIZE);
index = index + CHUNKSIZE;
}
System.arraycopy(fixedChunk, 0, responseBody, index, chunkIndex);

return responseBody;

}catch(SocketTimeoutException se)
{

}
catch(Exception e)
{

e.printStackTrace();
}finally
{
if(connection!=null)
connection.disconnect();
}

return null;
}

}

Please note this doesnot guarantee relief from OOM. It is not always a good idea to show large bitmaps.

Another option you can go after is use BitmapFactory.Options inSampleSize argument

Out of Memory error with Bitmap

OutofMemory occurs when your app exceeds memory allocated in heap. The bitmap is too large to fit in memory ie heap. In such a case you run out of memory. You need to scale down the bitmap and then use the same. For that check the link below

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html.

There is also a blog @ http://android-developers.blogspot.in/2009/01/avoiding-memory-leaks.html (avoiding memory leaks)

 public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){
try {
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);

//The new size we want to scale to
final int REQUIRED_WIDTH=WIDTH;
final int REQUIRED_HIGHT=HIGHT;
//Find the correct scale value. It should be the power of 2.
int scale=1;
while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT)
scale*=2;

//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}

Quoting from the docs

The BitmapFactory class provides several decoding methods (decodeByteArray(), decodeFile(), decodeResource(), etc.) for creating a Bitmap from various sources. Choose the most appropriate decode method based on your image data source. These methods attempt to allocate memory for the constructed bitmap and therefore can easily result in an OutOfMemory exception. Each type of decode method has additional signatures that let you specify decoding options via the BitmapFactory.Options class.

Setting the inJustDecodeBounds property to true while decoding avoids memory allocation, returning null for the bitmap object but setting outWidth, outHeight and outMimeType. This technique allows you to read the dimensions and type of the image data prior to construction (and memory allocation) of the bitmap.

Also check this link for memory management.

https://www.youtube.com/watch?v=_CruQY55HOk

Out of memory exception due to large bitmap size

Its not always a good idea to store bitmaps in memory. If you really want to do so then try using SoftReference for your map. Check this

make your map's value argument as SoftReference<Bitmap>. Then while searching in the map use this code snippet

@SuppressWarnings("unchecked")
public class DrawableManager {

@SuppressWarnings("rawtypes")
private final Map<String, SoftReference<Bitmap>> drawableMap;

@SuppressWarnings("rawtypes")
private DrawableManager() {
drawableMap = new HashMap<String, SoftReference<Bitmap>>();
}

static private DrawableManager _instance;

static public DrawableManager getInstance() {
if(_instance == null) {
_instance = new DrawableManager();
}
return _instance;
}

public Bitmap fetchBitmap(final String sURL) {
if(sURL.length() == 0)
return null;
Bitmap bm = null;
SoftReference<Bitmap> reference = drawbaleM.get(imagePath);
if(reference != null) bm = reference.get();
if(bm != null) {
return bm;
}

byte[] imageData = ThumbImg(sURL);

if(imageData == null)
return null;

if(imageData.length > 0) {
bm = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
if(bm != null) {
drawableMap.put(sURL, bm);
}
return bm;
}
else {
return null;
}
}

public void fetchBitmapOnThread(final String sURL, final ImageView imageView) {
if (drawableMap.containsKey(sURL)) {
imageView.setImageBitmap((Bitmap) drawableMap.get(sURL));
}

final Handler handler = new Handler() {
@Override
public void handleMessage(Message message) {
imageView.setImageBitmap((Bitmap) message.obj);
}
};

Thread thread = new Thread() {
@Override
public void run() {
Bitmap bitmap = fetchBitmap(sURL);
Message message = handler.obtainMessage(1, bitmap);
handler.sendMessage(message);
}
};
thread.start();
}

@SuppressWarnings("unused")
public static byte[] ThumbImg(String imgUrl)
{

//first check in the cache, if not available then store in the sd card memory
HttpURLConnection connection = null;
String userAgent = null;

try
{
URL url = new URL(imgUrl);
connection = ( HttpURLConnection ) url.openConnection();
if(userAgent != null) {
connection.setRequestProperty("User-Agent", userAgent);
}
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int CHUNKSIZE = 8192; //size of fixed chunks
int BUFFERSIZE = 1024; //size of reading buffer

int bytesRead = 0;
byte[] buffer = new byte[BUFFERSIZE]; //initialize buffer
byte[] fixedChunk = new byte[CHUNKSIZE]; //initialize 1st chunk
ArrayList<byte[]> BufferChunkList = new ArrayList<byte[]>(); // List of chunk data
int spaceLeft = CHUNKSIZE;
int chunkIndex = 0;

DataInputStream in = new DataInputStream(connection.getInputStream() );

while( ( bytesRead = in.read( buffer ) ) != -1 ) { //loop until the DataInputStream is completed
if(bytesRead > spaceLeft) {
//copy to end of current chunk
System.arraycopy(buffer, 0, fixedChunk, chunkIndex, spaceLeft);
BufferChunkList.add(fixedChunk);

//create a new chunk, and fill in the leftover
fixedChunk = new byte[CHUNKSIZE];
chunkIndex = bytesRead - spaceLeft;
System.arraycopy(buffer, spaceLeft, fixedChunk, 0, chunkIndex);
} else {
//plenty of space, just copy it in
System.arraycopy(buffer, 0, fixedChunk, chunkIndex, bytesRead);
chunkIndex = chunkIndex + bytesRead;
}
spaceLeft = CHUNKSIZE - chunkIndex;
}

if (in != null) {
in.close();
}

// copy it all into one big array
int responseSize = (BufferChunkList.size() * CHUNKSIZE) + chunkIndex;
Log.d("response size",""+responseSize);
byte[] responseBody = new byte[responseSize];
int index = 0;
for(byte[] b : BufferChunkList) {
System.arraycopy(b, 0, responseBody, index, CHUNKSIZE);
index = index + CHUNKSIZE;
}
System.arraycopy(fixedChunk, 0, responseBody, index, chunkIndex);

return responseBody;

}catch(SocketTimeoutException se)
{

}
catch(Exception e)
{

e.printStackTrace();
}finally
{
if(connection!=null)
connection.disconnect();
}

return null;
}

}

Please note this doesnot guarantee relief from OOM. It is not always a good idea to show large bitmaps.

Another option you can go after is use BitmapFactory.Options inSampleSize argument



Related Topics



Leave a reply



Submit