Suggestions to avoid bitmap Out of Memory error - android

I am working on an android application. The application has a view containing lots of image. I had an error, I will try to give as much information as possible hoping someone can give me some suggestions.
The application was working great on all the local testings. However, I received lots of crashes from users: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
This is the stack trace
0 java.lang.OutOfMemoryError: bitmap size exceeds VM budget
1 at android.graphics.Bitmap.nativeCreate(Native Method)
2 at android.graphics.Bitmap.createBitmap(Bitmap.java:507)
3 at android.graphics.Bitmap.createBitmap(Bitmap.java:474)
4 at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:379)
5 at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:498)
6 at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:473)
7 at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
8 at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:359)
9 at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:385)
My biggest problem is that I was not able to reproduce the issue locally even on old devices.
I have implemented lots of things to try to resolve this:
No memory leaks: I made sure there is no memory leaks at all. I removed the views when I dont need them. I also recycled all the bitmaps and made sure the garbage collector is working as it should. And I implemented all the necessary steps in the onDestroy() method
Image size scaled correctly: Before getting the image I get its dimension and calculate the inSampleSize.
Heap size: I also detect the Max Heap size before getting the image and make sure there is enough space. If there is not enough I rescale the image accordingly.
Code to calculate the correct inSampleSize
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
{
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if(height > reqHeight || width > reqWidth)
{
if(width > height)
{
inSampleSize = Math.round((float) height / (float) reqHeight);
}
else
{
inSampleSize = Math.round((float) width / (float) reqWidth);
}
}
return inSampleSize;
}
Code to get the bitmap
// decodes image and scales it to reduce memory consumption
private static Bitmap decodeFile(File file, int newWidth, int newHeight)
{// target size
try
{
Bitmap bmp = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), Uri.fromFile(file));
if(bmp == null)
{
// avoid concurrence
// Decode image size
BitmapFactory.Options option = new BitmapFactory.Options();
// option = getBitmapOutput(file);
option.inDensity = res.getDisplayMetrics().densityDpi < DisplayMetrics.DENSITY_HIGH ? 120 : 240;
option.inTargetDensity = res.getDisplayMetrics().densityDpi;
if(newHeight > 0 && newWidth > 0)
option.inSampleSize = calculateInSampleSize(option, newWidth, newWidth);
option.inJustDecodeBounds = false;
byte[] decodeBuffer = new byte[12 * 1024];
option.inTempStorage = decodeBuffer;
option.inPurgeable = true;
option.inInputShareable = true;
option.inScaled = true;
bmp = BitmapFactory.decodeStream(new FileInputStream(file), null, option);
if(bmp == null)
{
return null;
}
}
else
{
int inDensity = res.getDisplayMetrics().densityDpi < DisplayMetrics.DENSITY_HIGH ? 120 : 240;
int inTargetDensity = res.getDisplayMetrics().densityDpi;
if(inDensity != inTargetDensity)
{
int newBmpWidth = (bmp.getWidth() * inTargetDensity) / inDensity;
int newBmpHeight = (bmp.getHeight() * inTargetDensity) / inDensity;
bmp = Bitmap.createScaledBitmap(bmp, newBmpWidth, newBmpHeight, true);
}
}
return bmp;
}
catch(Exception e)
{
Log.e("Error calling Application.decodeFile Method params: " + Arrays.toString(new Object[]{file }), e);
}
return null;
}
Code to calculate image size based on Heap size for older devices
private void calculateImagesSize()
{
// only for android older than HoneyComb that does not support large heap
if(Build.VERSION.SDK_INT < Constants.HONEYCOMB)
{
long maxHeapSize = Runtime.getRuntime().maxMemory();
long maxImageHeap = maxHeapSize - 10485760;
if(Application.getResource().getDisplayMetrics().densityDpi >= DisplayMetrics.DENSITY_XHIGH)
{
maxImageHeap -= 12 * 1048576;
}
if(maxImageHeap < (30 * 1048576))
{
int screenHeight = Math.min(Application.getResource().getDisplayMetrics().heightPixels, Application.getResource()
.getDisplayMetrics().widthPixels);
long maxImageSize = maxImageHeap / 100;
long maxPixels = maxImageSize / 4;
long maxHeight = (long) Math.sqrt(maxPixels / 1.5);
if(maxHeight < screenHeight)
{
drawableHeight = (int) maxHeight;
drawableWidth = (int) (drawableHeight * 1.5);
}
}
}
}
I think the problem is with the Heap, maybe sometimes the os doesn't allow the application to use the maxheapsize. Also my biggest problem is that I was not able to reproduce the issue, so when I try a fix I have to wait a little to see if users are still getting the error.
What more could I try to avoid Out of memory issues? Any suggestions would be greatly appreciated. Thanks a lot

just use this function to decode...this is perfect solution for your error..because i also getting same error and i got this solution..
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;
}

Hi you have to decode the file . for this try with the following method.
public static Bitmap new_decode(File f) {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
o.inDither = false; // Disable Dithering mode
o.inPurgeable = true; // Tell to gc that whether it needs free memory,
// the Bitmap can be cleared
o.inInputShareable = true; // Which kind of reference will be used to
// recover the Bitmap data after being
// clear, when it will be used in the future
try {
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 300;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 1.5 < REQUIRED_SIZE && height_tmp / 1.5 < REQUIRED_SIZE)
break;
width_tmp /= 1.5;
height_tmp /= 1.5;
scale *= 1.5;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
// o2.inSampleSize=scale;
o.inDither = false; // Disable Dithering mode
o.inPurgeable = true; // Tell to gc that whether it needs free memory,
// the Bitmap can be cleared
o.inInputShareable = true; // Which kind of reference will be used to
// recover the Bitmap data after being
// clear, when it will be used in the future
// return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
try {
// return BitmapFactory.decodeStream(new FileInputStream(f), null,
// null);
Bitmap bitmap= BitmapFactory.decodeStream(new FileInputStream(f), null, null);
System.out.println(" IW " + width_tmp);
System.out.println("IHH " + height_tmp);
int iW = width_tmp;
int iH = height_tmp;
return Bitmap.createScaledBitmap(bitmap, iW, iH, true);
} catch (OutOfMemoryError e) {
// TODO: handle exception
e.printStackTrace();
// clearCache();
// System.out.println("bitmap creating success");
System.gc();
return null;
// System.runFinalization();
// Runtime.getRuntime().gc();
// System.gc();
// decodeFile(f);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}

By Reducing/Scale size of the Image you can get rid out of the Out of Memory Exception,
Try this
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 6;
Bitmap receipt = BitmapFactory.decodeFile(photo.toString(),options); //From File You can customise on your needs.

I wrote a summary of suggestions in another StackOverFlow question: Android: BitmapFactory.decodeStream() out of memory with a 400KB file with 2MB free heap

actually the problem is with the development os. In android unlike iOS , google people develop this based on camera resolution. Bitmaps take up a lot of memory, especially for rich images like photographs.Different cameras captures images with different pixels(different mobiles have different camera pixel capacity). Here in android based on that pixels only the captured image will take memory. so obviously a high resolution image will not uploaded by a phone with low pixel capacity.
In android os allocates utmost 16MB to every application. If the uploaded image takes more than this then java.lang.OutofMemoryError: bitmap size exceeds VM budget will occur and application crashes.
refer this
http://developer.android.com/training/displaying-bitmaps/index.html

If u want to avoid OOM, u can catch OOM and increase the sampleSize until the image can be resolved:
private Bitmap getBitmapSafely(Resources res, int id, int sampleSize) {
// res = context.getResources(), id = R.drawable.yourimageid
Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inSampleSize = sampleSize;
try {
bitmap = BitmapFactory.decodeResource(res,
id, options);
} catch (OutOfMemoryError oom) {
Log.w("ImageView", "OOM with sampleSize " + sampleSize, oom);
System.gc();
bitmap = getBitmapSafely(res, id, sampleSize + 1);
}
return bitmap;
}
Hope it helps.
It is not suitable to catch the Error, just a workaround.

Related

Converting Bitmap to Byte Array Occupying Much Memory

I Need to Convert Bitmap to Byte Array Inorder to Upload to server i was able to achieve the same by converting Bitmap to Byte Array and again converting it to Base 64 String by following instructions here it worked well but in the worst case in my galaxy s2 mobile if the image size is of 6MB with 72 Pixels/Inch Resolution it is occupying around 600MB of RAM and app getting crash with OutOfMemoryException, i tried to upload by compressing the bitmap it worked fine but in my project requirement i need to upload the image as is i.e. with out any compression the original image
please help me how to achieve this whether it is possible or not
Thanks in Advance
For uploading convert to jpeg stream
BufferedStream bs = //...
then call bitmap.compress("JPEG", 0, length, bs)
convert bs to array ad upload that to server
Android application allocations memory heap is something limited. especially on low ram memory devices.
you are doing two different common mistakes:
first: regarding the upload issue - you are holding Bitmap object of a full size image (probably captured with the camera) . this is mistake at the first place. if you have to show on the user interface the captured image - you should load scaled version bitmap according to the required display size(the ImageView width and height..) from the captured image file that just captured and created:
public static Bitmap getSampleBitmapFromFile(String bitmapFilePath, int reqWidth, int reqHeight) {
try {
File f = new File(bitmapFilePath);
ExifInterface exif = new ExifInterface(f.getPath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int angle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
angle = 90;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
angle = 180;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
angle = 270;
}
Matrix mat = new Matrix();
mat.postRotate(angle);
// calculating image size
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, options);
int scale = calculateInSampleSize(options, reqWidth, reqHeight);
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
Bitmap correctBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);
return correctBmp;
} catch (IOException e) {
Log.e(TAG, "cant butmaputils.getSampleBitmapFromFile failed with IO exception: ", e);
if (e != null)
e.printStackTrace();
} catch (OutOfMemoryError oom) {
Log.e(TAG, "cant butmaputils.getSampleBitmapFromFile failed with OOM error: ");
if (oom != null)
oom.printStackTrace();
} catch (Exception e) {
Log.e(TAG, "cant butmaputils.getSampleBitmapFromFile failed with exception: ", e);
}
Log.e(TAG, "butmaputils.getSampleBitmapFromFilereturn null for file: " + bitmapFilePath);
return null;
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and
// width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
by that alone - you reduce dramatically the pressure on your memory allocation heap, because instead of allocation (for example) 1080x1920 integers, you could allocate just 100x200 if that's your imageView screen dimentions.
second: uploading to server: you should upload to your server a direct stream from the large original file instead of loading it to memory as Bitmap + decode it to base 64 or. the best way to do it is by using Multipart Entity.
using this approch, not limiting you at all, and even if you want to upload 100M-1000M file - it does not matters, and don't have impact on the memory allocation heap.
for more reading, I recommend you to read - http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Check Android image size before load to memory

I've created small app that works with images from gallery or camera.
It all works fine, but.
On device with small screen and small memory size (HTC Desire) I have some images downloaded in full size from other mobile phone, and they are much larger (8MP camera on that phone).
If I try to load that, for my small camera huge image, it will crash immediately.
So, how to implement some kind of check and to downsize that image, but still load it properly?
I do scale images down after they are loaded, but this is something that should be done before the crash appears.
Tnx.
InputStream in = null;
try {
in = getContentResolver().openInputStream(data.getData());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// get picture size.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, options);
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// resize the picture for memory.
int screenH = getResources().getDisplayMetrics().heightPixels; //800
int screenW = getResources().getDisplayMetrics().widthPixels; //480
int width = options.outWidth / screenW;
int height = options.outHeight / screenH;
Log.w("Screen Width", Integer.toString(width));
Log.w("Screen Height", Integer.toString(height));
int sampleSize = Math.max(width, height);
options.inSampleSize = sampleSize;
options.inJustDecodeBounds = false;
try {
in = getContentResolver().openInputStream(data.getData());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// convert to bitmap with declared size.
Globals.INSTANCE.imageBmp = BitmapFactory.decodeStream(in, null, options);
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
you can avoid load the bitmap in memory just setting
inJustDecodeBounds = true
inJustDecodeBounds will allow you to decode only the bounds of the image without decode it. Given height and width of your bitmap you can downsampling it using.
inSampleSize
as the doc stays:
If set to a value > 1, requests the decoder to subsample the original
image, returning a smaller image to save memory.
int tmpWidth = bitmapWidth;
int tmpHeight = bitmapHeigth;
int requiredSize = ...
while (true) {
if (tmpWidth / 2 < requiredSize
|| tmpHeight / 2 < requiredSize)
break;
tmpWidth /= 2;
tmpHeight /= 2;
ratio *= 2;
}
EDIT: for a 32 bit Bitmap the memory required is width * height * 4

Android bitmap size exceeds VM budget error

Android bitmap size exceeds VM budget.
My app is getting this error frequently. I have two questions.
Do I need to recycle my about activity (it contains some imageviews and buttons and textViews)?
What is the difference between .recycle(); and between system.gc(); ?
You should always try and recycle Bitmaps afte you have used them.
As far as I understand, you should try and avoid calling system.gc().
Calling recycle() will allow the bitmap object to be garbage collected.
I hope this helps.
I got the same problem while picking images from camera.
I resized the bitmap of image using following code:
Bitmap bitmap = resizeBitMapImage(picturePath, 75, 91);
profilePic.setImageBitmap(bitmap);
private Bitmap resizeBitMapImage(String filePath, int targetWidth,
int targetHeight) {
Bitmap bitMapImage = null;
// First, get the dimensions of the image
Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
double sampleSize = 0;
// Only scale if we need to
// (16384 buffer for img processing)
Boolean scaleByHeight = Math.abs(options.outHeight - targetHeight) >= Math
.abs(options.outWidth - targetWidth);
if (options.outHeight * options.outWidth * 2 >= 1638) {
// Load, scaling to smallest power of 2 that'll get it <= desired
// dimensions
sampleSize = scaleByHeight ? options.outHeight / targetHeight
: options.outWidth / targetWidth;
sampleSize = (int) Math.pow(2d,
Math.floor(Math.log(sampleSize) / Math.log(2d)));
}
// Do the actual decoding
options.inJustDecodeBounds = false;
options.inTempStorage = new byte[128];
while (true) {
try {
options.inSampleSize = (int) sampleSize;
bitMapImage = BitmapFactory.decodeFile(filePath, options);
break;
} catch (Exception ex) {
try {
sampleSize = sampleSize * 2;
} catch (Exception ex1) {
}
}
}
return bitMapImage;
}

android : bitmap size exceeds VM budget

I am using android Camera. Also using auto focusing feature. After capturing image more then 10 times getting the following exception:
Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
my source code is following:
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
try {
// Bitmap bmp = BitmapFactory.decodeStream(new
// ByteArrayInputStream(data));
Utility.gc();
Bitmap bmp = decodeFile(data);
Utility.gc();
// BitmapFactory.decodeByteArray(data, 0,
// data.length, o);
// Bitmap bmpCompressed = rotateBitmap(bmp, 90, 320, 430);
Bitmap bmpCompressed = Bitmap.createScaledBitmap(bmp, 430, 320,
true);
bmp.recycle();
Utility.gc();
writeBmp(bmpCompressed);
bmpCompressed.recycle();
Utility.gc();
} catch (Exception e) {
Log.e(Constants.TAG, e.getMessage(), e);
} finally {
isImageCapture = true;
}
}
};
private Bitmap decodeFile(byte[] buffer) {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new ByteArrayInputStream(buffer), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 70;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new ByteArrayInputStream(buffer),
null, o2);
}
how can i get rid from the above mention exception?
Use
Options.inSampleSize
// Decode image size
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inJustDecodeBounds = true;
options.inSampleSize = 8;
BitmapFactory.decodeStream(new ByteArrayInputStream(buffer), null, options);
Take a look at this bug report:
http://code.google.com/p/android/issues/detail?id=8488
There's a known bug in Android relating to this error message, which doesn't seem to be accepted by Google. There are workarounds in the bug report if this is the problem you're having. I've used them to clear up similar issues.
You can try to do the scaling yourself using BitmapFactory.Options and BitmapFactory.decodeByteArray():
BitmapFactory.Options options = new BitmapFactory.Options;
options.inSampleSize = 4; // might try 8 also
BitmapFactory.decoderByteArray(data, 0, data.length, options);
There are also other options in BitmapFactory.Options to play around with.
You are probably running out of memory because you create a full decoded bitmap before you do the scaling.
Use less memory.
It definitely sounds like you have a memory leak somewhere if it works until the 10th picture is taken. Use the DDMS tools in Eclipse to track memory to try to see where it's happening.

createBitmap causes an outofmemory error

In my app, I am creating a bitmap from its colors code like this :
int width=getImageWidth();
int height=getImageHeight();
int[] array=getbitmap();
int[] colorsAsIntegers = new int[width*height];
int i = 0;
int j = 0;
while (i<width*height*4) {
colorsAsIntegers[j] = Color.argb(array[i], array[i+1], array[i+2],
array[i+3]);
i += 4;
j++;
}
myBitmap=Bitmap.createBitmap(colorsAsIntegers,
width,
height,
Bitmap.Config.ARGB_8888);
And often I get the outofmemoryerror :(
So how can I use the BitmapFactory optimisation to avoid this problem?
because I don't have an input stream or a file, I only have an array
containing my pixels
Thank you
Couple of things....
Android has really serious problems with bitmaps. They're allocated in non-garbage collected memory.Which is fine. They do get garbage-collected when the owning Bitmap gets collected. What they don't do is relocate while the heap is being compacted. This can cause premature out-of-memory errors due to heap fragmentation. The solution: call Bitmap.recycle as soon as you're done with a bitmap. This frees the non-gc memory, and reduces fragmentation problems.
You can also reduce memory pressure by creating an empty bitmap, and then copying pixels in row by row from a single small buffer. Java gc doesn't particularly like huge arrays either (although large arrays can be relocated during a gc). The performance hit is tiny, and negligible compared to the performance hit from a premature garbage collect. Doing that will reduce your memory usage 49.99 percent.
Try downsampling your image as shown here
please decode the file first,for this use this code:
public static Bitmap new_decode(File f) {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
o.inDither = false; // Disable Dithering mode
o.inPurgeable = true; // Tell to gc that whether it needs free memory,
// the Bitmap can be cleared
o.inInputShareable = true; // Which kind of reference will be used to
// recover the Bitmap data after being
// clear, when it will be used in the future
try {
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 300;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 1.5 < REQUIRED_SIZE && height_tmp / 1.5 < REQUIRED_SIZE)
break;
width_tmp /= 1.5;
height_tmp /= 1.5;
scale *= 1.5;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
// o2.inSampleSize=scale;
o.inDither = false; // Disable Dithering mode
o.inPurgeable = true; // Tell to gc that whether it needs free memory,
// the Bitmap can be cleared
o.inInputShareable = true; // Which kind of reference will be used to
// recover the Bitmap data after being
// clear, when it will be used in the future
// return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
try {
// return BitmapFactory.decodeStream(new FileInputStream(f), null,
// null);
Bitmap bitmap= BitmapFactory.decodeStream(new FileInputStream(f), null, null);
System.out.println(" IW " + width_tmp);
System.out.println("IHH " + height_tmp);
int iW = width_tmp;
int iH = height_tmp;
return Bitmap.createScaledBitmap(bitmap, iW, iH, true);
} catch (OutOfMemoryError e) {
// TODO: handle exception
e.printStackTrace();
// clearCache();
// System.out.println("bitmap creating success");
System.gc();
return null;
// System.runFinalization();
// Runtime.getRuntime().gc();
// System.gc();
// decodeFile(f);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
You can use android:largeHeap="true" to request a larger heap size
mention this in manifest file to use large heap.

Categories

Resources