ViewFlipper : Bitmap size exceeds VM budget - android

I am using a ViewFlipper to show some images from db.
I am taking the blob values to an ArrayList then, converting each byte array to bitmap.
And then I am setting these each bitmap to each Imageview of ViewFlipper.
If the count of images is below 10, then it is working fine. But if it exceeds 10, then suddently it caught an Exception :
OutOfMemoryException : Bitmap size exceeds VM budget.
When I use inSampleSize of BitmapFactory.Options, it is working fine.But I want to display the image with actual height and width. How can I do this without exception?
My code is :
ViewFlipper vfImage = (ViewFlipper)findViewById(R.id.vfImage);
for(Photos objPhotos :arPhotos)
{
byte[] btImages = null;
Bitmap bitmapImage = null;
btImages = objPhotos.getImage();
ImageView imgPhoto= new ImageView(this);
bitmapImage = BitmapFactory.decodeByteArray(btImages, 0, btImages.length);
imgPhoto.setImageBitmap(bitmapImage);
vfImage.addView(imgPhoto);
}
vfImage.startFlipping();
Please help me..
Thank you...

You should think about unloading the bitmaps that are currently not shown, to keep in memory only those that are on the screen right now. Good luck!

Once you have completed using your bitmaps, try and make it null like this,
Bitmap bitmapImage = null;
Try adding the above line in the last line of your for loop. This means each and every time the bitmap is made null, which reduces the memory being captured by your bitmap. Also try providing
bitmapImage.recycle();
This will recycle the bitmap and provide you with free memory.
Also you can refer to my question here

Related

Dealy in wallpaperManager.setBitmap (bitmap)

I am trying to change wallpaper using a widget.
I use the code below to achieve that:
But there is an certain delay every time the code is executed.
Is there any way to avoid this delay.
...
WallpaperManager wallpaperManager = WallpaperManager.getInstance (context);
Bitmap bitmap = BitmapFactory.decodeStream (new FileInputStream (file));
//here #file is fetched from a phone storage
wallpaperManager.setBitmap (bitmap);
...
I see four options which might help here:
Reduce the size of the bitmap before saving it to the file
Preload the bitmap (well, if possible)
Do all this asyncronous
Make sure your heap is as empty as possible before loading the bitmap. If android has to free heap while decoding a large bitmap it will slow down noticeably

Android OutOfMemory Error while loading Bitmaps

In Application i need to load a few Bitmaps on create.
To save Memory i'm loading one image, rescale it by creating a scaledBitmap out of it, recycle the unscaled Bitmap on so on:
bmpUnscaled = BitmapFactory.decodeResource(getResources(), R.drawable.cultivation_plant_resized_1);
plant1 = Bitmap.createScaledBitmap(bmpUnscaled,(int) plant[0].getWidth(), (int) plant[0].getHeight(), true);
bmpUnscaled.recycle();
System.gc();
bmpUnscaled = BitmapFactory.decodeResource(getResources(), R.drawable.cultivation_plant_resized_2);
plant2 = Bitmap.createScaledBitmap(bmpUnscaled,(int) plant[0].getWidth(), (int) plant[0].getHeight(), true);
bmpUnscaled.recycle();
System.gc();
and so on...
I am doing this 10 Images, wich are scaled relative to the screen resolution.
The orginal Image is a PNG (570x900 (max. 660KB))
Does anybody have some Ideas to save Memory simply?
I'm desperate right now...
Have you read: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html?
The article contain great information about working with BitMap.

Android. OutOfMemory problems

I require displaying many images in my application. These being jpgs and pngs and i'm loading them inside ImageViews like so:
tile.setImageResource(R.drawable.tile_highlight);
I am currently having OutOfMemory problems (java.lang.OutOfMemoryError: bitmap size exceeds VM budget)
I've searched and found some other posts, they all suggest that you should recycle the bitmap of an ImageView manually, like so: ((BitmapDrawable)imageView.getDrawable()).getBitmap().recycle(); which will dump it from memory.
BUT in my case, being that i'm not using setBitmap() to load the images onto the ImageView objects, when i try and run the above code, it returns NullPointerException, more precisely, the method getBitmap() returns null, there is no bitmap ?!?!
Do i need to go back in my code and change the way i load all the images in the ImageViews, and then try with the recycle() method? Or how can i free up the memory so it doesn't crash anymore?
EDIT
I've tried something like so: imageView.setImageResource(-1); in hopes it will remove the image from memory and replace it with ... null or something, but it seems it doesn't help the cause.
It would be helpful if you could post some of your code. Specifically, how you're setting the images of the ImageView objects. If you're not using bitmaps, I would expect that getBitmap() will return null. However, if you're using another sort of Drawable or otherwise to set the image, there's likely a similar route to take that doesn't involve bitmaps.
EDIT:
Alright, give this a shot. You can create a Bitmap from a resource like this:
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);
Then, use the bitmap like this, considering img is your ImageView:
img.setImageBitmap(bm);
//Do some stuff with it, then when memory gets low:
((BitmapDrawable)img.getDrawable()).getBitmap().recycle();
Of course, this is considering that you're in an activity. If not, you'll have to get a handle on the context and replace getResources() with context.getResources().
If you want to set an image from drawable to ImageView, Do not use ImageView.setImageResource(int resId) directly with drawable id. Instead get Scaled down bitmap(if applicable) and set it to imageView. like this:
this works for me.
iv.setImageBitmap(decodeResource(getResources(), R.drawable.big_image));
private static Bitmap decodeResource(Resources res, int id) {
Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
for (options.inSampleSize = 1; options.inSampleSize <= 32; options.inSampleSize++) {
try {
bitmap = BitmapFactory.decodeResource(res, id, options);
Log.d(TAG_LOG, "Decoded successfully for sampleSize " + options.inSampleSize);
break;
} catch (OutOfMemoryError outOfMemoryError) {
// If an OutOfMemoryError occurred, we continue with for loop and next inSampleSize value
Log.e(TAG_LOG, "outOfMemoryError while reading file for sampleSize " + options.inSampleSize
+ " retrying with higher value");
}
}
return bitmap;
}
source: https://codingjunkiesforum.wordpress.com/2014/06/12/outofmemory-due-to-large-bitmap-handling-in-android/
try
((BitmapDrawable)im.getBackground()).getBitmap().recycle();
I think you should display images with lower resolution. This would resolve the OOM problem. You can also read Android Developer Guideline link
OutOfMemory error comes when you dont free bitmap once its being used(may be large size) so to prevent this we should keep in mind of re-sizing,taking weakreferences etc.
but there are good libraries available which is taking care all problem one of them is Picasso. please have a look at-
loading images using Picasso

Getting error: external allocation too large for this process

I am trying to implement the code below converting a image path into a bitmap to display on my activity. I am getting the below error. I have tried a bunch of different solutions but none are working
Strange out of memory issue while loading an image to a Bitmap object
Android: Resize a large bitmap file to scaled output file
OutOfMemoryError: bitmap size exceeds VM budget :- Android
Error:
E/dalvikvm-heap(19252): 12742656-byte external allocation too large for this process.
for(int i = 0; i < numItems; i++) {
File imgFile = new File(photoPaths.get(i));
if(imgFile.exists())
{
images[i] = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
}
You may consider loading them in a more just-in-time kind of approach, or using the inSampleSize option of the bitmap factory (i.e., you'd pass a BitmapFactory.Options in to the factory with inSampleSize set to, ideally, a power of 2). Also make sure you set inPurgeable true.
In the event that you are pulling these images from the MediaStore's ContentProvider, you can also use thumbnails.
Perhaps you can tell us more about your use case so we can better help.
You're trying to load multiple large bitmaps, at least one of which is ~12MB. They're too large. The solutions you posted DO work. Resize your bitmaps to something much smaller, especially as you're just displaying the bitmaps on the screen.

Efficient way of using images in android (`Drawable` of `bitmap` )

Image is always making problem for me always :(.
java.lang.OutOfMemoryError: bitmap size exceeds VM budget - android is the main problem even if i use very small image.
Please suggest me some tips in using Image. (link, tutorials, sample projects etc...)
Some of my quires are
Which should i prefer Drawable or bitmap
BitmapFactory.decodeFile or Drawable.createFromPath
BitmapFactory.decodeStream or Drawable.createFromStream
How to retain Drawable and Bitmap while rotating.
Is it a good idea to save or pass Drawable or Bitmap through intent
that all depends on what exactly you want to do with your image. bitmapfactory is for creating bitmaps. and drawable objects are used when you want to draw something :)
what do you want to do with your bitmap after you have created it in the memory?
as for rotating, you should take care of onRestoreInstanceState and onSaveInstanceState. something like this:
private static final String PHOTO_TAKEN = "photo_taken";
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
if (savedInstanceState.getBoolean(PHOTO_TAKEN)) {
// do something if you have your pic here
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean(PHOTO_TAKEN, mPhotoTaken); // saving the state of the image (if the photo is taken or not in this case)
}
as for passing through extras... i bet it's a bad idea as you already getting outofmemory exception.... you can pass a reference to that image, like ID of path or something else.
UPDATE:
in one of the projects i was trying to show the image taken from the cam in an ImageView control ion my activity and got that outofmemory exception. the reason was that it was putting the whole big image into memory. the workaround was quite simple: i decreased the image size:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile(mImagePath, options);
To avoid out of memory error we can use BitmapRegionDecoder ( to decode the original image which is larger than maximum texture limit 2048x2048 ) or we can decode it by specifying the coordinates from the original bitmap. (for e.g coordinates of each quarter)

Categories

Resources