Reusing bitmaps for BitmapFactory.decodeFile and BitmapFactory.Options.inBitmap - android

My app decodes a lot of bitmaps from sd card, so I want to reuse existing bitmaps to decrease GC work. I see examples from Android Training and this video and it works perfect for BitmapFactory.decodeResource, but not for BitmapFactory.decodeFile. I use next code:
private void testBitmapReusing() {
BitmapFactory.Options options = newOptions();
Bitmap bitmap = decode(options);
options.inBitmap = bitmap;
bitmap = decode(options);
}
private BitmapFactory.Options newOptions() {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
options.inMutable = true;
return options;
}
private Bitmap decode(BitmapFactory.Options options) {
return BitmapFactory.decodeFile("/mnt/sdcard/sun.jpg", options);
// return BitmapFactory.decodeResource(getResources(), R.drawable.sun, options);
}
Commented code (BitmapFactory.decodeResource) works as expected, it decodes new bitmap using existing bitmap. But uncommented code (BitmapFactory.decodeFile) doesn't decode new bitmap. It just writes to log message "E/BitmapFactory: Unable to decode stream: java.lang.IllegalArgumentException: Problem decoding into existing bitmap"
So where is my mistake?
UPDATE
I realize my fail. I try to decode GIF images, but it is impossible reuse bitmaps in GIF format. The docs says:
The source content must be in jpeg or png format (whether as a resource or as a stream)

It might not be what you're looking for directly, but the alternate approach would be to create a Map (e.g. a HashMap) and add the resources to that as soon as they're being called. Something like this:
HashMap<String, Bitmap> map = new HashMap<String, Bitmap>();
public Bitmap loadBitmap(String location) {
if (map.containsKey(location))
return map.get(location);
Bitmap bmp = yourDecodeMethod(location);
map.put(location, bmp);
return bmp;
}
This is the same way I use when I do not want to reload resources every time. Sorry if this is not what you wanted to do, I tried my best to understand your actual goal :)
Good luck!

Related

Does anyone know if there is a limitation on the size of the file BitmapFactory.decodeFile can decode?

I use the following code. With large image files it returns null, small ones display. Does anyone know if there is a size limitation with BitmapFactory.decodeFile?
File imgFile = new File(path);
if(imgFile.exists())
{
Bitmap myBitmap=null;
myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView imageView=(ImageView)findViewById(R.id.imageView1);
if(myBitmap!=null) {
imageView.setImageBitmap(myBitmap);
}
else {
Toast.makeText(MainActivity.this, "NULL", Toast.LENGTH_LONG).show();
}
} else Toast.makeText(MainActivity.this, "NO FILE", Toast.LENGTH_LONG).show();
how large is it? how many MBs?
do you get an OutOfMemoryException because each mobile app have specific amount of memory (heap size) and a large bitmap might exceed this limit, so u get a null
EDIT:
unless the big bitmap by chance, is corrupted of some weird format that the app could not decode? try another large bitmap
From experience there is a limit and when you hit that resolution limit for openGL you will see the following type of error message:
Bitmap too large to be uploaded into a texture (4128x2322, max=4096x4096)
You may also get a OutOfMemoryException error like many have noted.
First of all you should do this:
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, null, options);
final int width = options.outWidth;
final int height = options.outHeight;
Now that you have the width and height, it's time to make an intelligent decision about how to rescale it.
I would at this point look at the full code example (really only two methods to copy/paste) from Google at Loading Large Bitmaps Efficiently.

Custom GridView causes out of memory error while loading lots of images

I have a custom layout for gridview. Each raw of gridview contains a progressbar, two imageviews and two text views. The image size are thumbnail sizes. While i have to load lots of bitmaps eg:- 500 images , which causes out of memory error. the images are first time loading from Internet and then which is stored in SD card and the next time when you loading the gridview which is loading from SD card. How to overcome this issue. I have found lots of answers to overcome out of memory if the gridview is inflated with single imageview. Please suggest me how to overcome this issue while using custom layout.Let me know if you know any example projects that handle this out of memory error in gridview while inflating custom layout in gridview.
It would be best to create a class that extends Application . This
application class will give you onlowmemory() callback whenever
application goes low memory. on there you can write
public void onLowmemory() {
Runtime.getRuntime().gc(); }
which will invoke system GC method. Upon executing garbage collector
android will garbage all unused objects.
There is another way to solve this problem. In animation you can call
Runtime.getRuntime().gc(); to invoke garbage collector. also in
activity onDestroy() method u can call Runtime.getRuntime().gc();
so your problem will be solved
I am facing the same issue.
The OutOfMemoryError is usually caused by
The first approach is to
BitmapFactory.decodeFile(srcImg);
Since images are transformed into bitmap before displaying, lots of big bitmap usually cause the error.
To overcome this, I added the following function
public static Bitmap decodeWithBounds(String srcImg, int bounds) {
if (bounds > 0){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(srcImg, options);
if (options.outHeight > bounds || options.outWidth > bounds){
options.inSampleSize = Math.max(options.outHeight/bounds, options.outWidth/bounds);
}
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(srcImg, options);
} else {
return BitmapFactory.decodeFile(srcImg);
}
}
I use this function to decode bitmap, with bounds = grid size.
This solved most problem.
For very low end device, add a try{} catch (OutOfMemoryError e){} ...
try this code set in image for gridview out of memory
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 8;//you set size qulity for image(2,3,,4,5,6,7,8 etc..)
Bitmap preview_bitmap=BitmapFactory.decodeStream(is,null,options);
set this bitmap in your imageview
Solve the out of memory exception issue with this code
Bitmap bitmap = BitmapFactory.decodeStream(is);
if (bitmap != null) {
Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, 100, 100, true);
imageView = new ImageView(mContext);
imageView.setImageBitmap(resizedBitmap);
}

Bitmap out of memory

I'm tring to get image from gallery (by intent).
I got this error:
985120-byte external allocation too large for this process.
Out of memory: Heap Size=4871KB, Allocated=2472KB, Bitmap Size=19677KB
VM won't let us allocate 985120 bytes
That's my code where I get image:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
....
mBitmap = Media.getBitmap(this.getContentResolver(), data.getData());
...
}
How can i solve it ?
-------- UPDATE ---------
I noticed that if I select a pre-existent image (HTC photo installed) I get this error. If I select image picked from camera all works fine.
So, I change my code according to this http://developer.android.com/training/displaying-bitmaps/load-bitmap.html:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
InputStream stream = getContentResolver().openInputStream(data.getData());
mBitmap = BitmapFactory.decodeStream(stream,null,options);
stream.close();
But now the bitmap is NULL !!!
It looks like your application uses a lot of high-res bitmap (the bitmap memory partition is 19677KB). The sie of 'heap' and 'allocated' are quite normal, and should not be of any problem. Make sure that you remove unused bitmap from memory. You can free a bitmap from memory by calling bitmap.recycle(), or setting the reference to it to null. Take a look at LruCache if you want to cache bitmaps for performance reasons.
I always wrap decoding in a while loop increasing the inSampleSize and catching OutOfMemoryError. This will give you the maximum possible resolution image. Always use LRU caches!
Bitmap image;
boolean success = false;int counter = 0;
while (success == false && counter < 10)
{
try
{
image = BitmapFactory.decodeFile(photoPath, options);
success = true;
}
catch(OutOfMemoryError e)
{
System.gc();
options.inSampleSize++;
counter++;
}
}

Error:Bitmap size Exceed VM budget, when converting image to ARGB_8888

Here is my code:
File file = new File(Path to Jpeg File size is 700kb);
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(file));
}
catch (Exception e) {
// TODO: handle exception
}
bitmap =BitmapFactory.decodeStream(in);
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Please help i get error in this copy line i want to make its ARGB_8888 image.Need Help :(
You need to reduce the memory usage.
From you code, you first decode stream to one bitmap, and then copy it, which means you create two large bitmap objects.
You don't need to decode and then copy it, you can try
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888
// You can try value larger than 1
options.inSampleSize = 2 // If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory.
// Decode bitmap
bitmap = BitmapFactory.decodeStream(in, null, options)
In this case, there's only one bitmap created. And you set inSampleSize to large values to reduce the loaded bitmap size.

OutOfMemory exception when loading bitmap from external storage

In my application I load a couple of images from JPEG and PNG files. When I place all those files into assets directory and load it in this way, everything is ok:
InputStream stream = getAssets().open(path);
Bitmap bitmap = BitmapFactory.decodeStream(stream, null, null);
stream.close();
return new BitmapDrawable(bitmap);
But when I try to load the exact same images from sd card, I get an OutOfMemory exception!
InputStream stream = new FileInputStream("/mnt/sdcard/mydata/" + path);
Bitmap bitmap = BitmapFactory.decodeStream(stream, null, null);
stream.close();
return new BitmapDrawable(bitmap);
This is what I get in the log:
11-05 00:53:31.003: ERROR/dalvikvm-heap(13183): 827200-byte external allocation too large for this process.
11-05 00:53:31.003: ERROR/GraphicsJNI(13183): VM won't let us allocate 827200 bytes
...
11-05 00:53:31.053: ERROR/AndroidRuntime(13183): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
11-05 00:53:31.053: ERROR/AndroidRuntime(13183): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
...
Why can this happen?
UPDATE: Tried both of these on real device - it seems that I can't load more than 12MB of bitmaps into whatever is called "external memory" (this is not an sd card).
I tried all the approaches mentioned here & at other resources but I came to the inference that setting ImageView's reference to null will solve the issue:
public Bitmap getimage(String path ,ImageView iv)
{
//iv is passed to set it null to remove it from external memory
iv=null;
InputStream stream = new FileInputStream("/mnt/sdcard/mydata/" + path);
Bitmap bitmap = BitmapFactory.decodeStream(stream, null, null);
stream.close();
stream=null;
return bitmap;
}
& you are done!
Note:Though it may solve above problem but I would suggest you to check Tom van Zummeren 's optimized image loading.
And also check SoftReference: All SoftReferences pointing to softly reachable objects are guaranteed to be cleared before the VM will throw an OutOfMemoryError.
When doing a lot with bitmaps, don't debug the app - just run it. The debugger will leave memory leaks.
Bitmaps are very expensive. If possible, scale them down on load by creating BitmapFactory.Options and setting inSampleSize to >1.
EDIT: Also, be sure to check your app for memory leaks. Leaking a Bitmap (having static Bitmaps is an excellent way to do that) will quickly exhaust your available memory.
Probably nothing wrong with your API usage, I guess all we can do is infer that using the AssetManager involves less behind-the-scenes heap allocation than opening a random file from the SD card.
800KB is a serious allocation in anybody's book... this will doubtless be for the decompressed image pixels. Given that you know the size of the image, what depth is it? If it's 32bpp then try overriding that using inPreferredConfig.
This is a fairly common issue which all of us face while loading images from the sdcard.
The solution as I found was to use inJustDecodeBounds first while loading the image using decodeFileDescriptor . That would not actually decode the image, but give the image size. Now I can scale it appropriately(using the options) so as to resize the image for the display area. Its needed because low memory on the phone can be easily taken over by your 5MP image. This I believe is the most elegant solution.
There are two issues here....
Bitmap memory isn't in the VM heap but rather in the native heap - see BitmapFactory OOM driving me nuts
Garbage collection for the native heap is lazier than the VM heap - so you need to be quite aggressive about doing bitmap.recycle and bitmap =null every time you go through an Activity's onPause or onDestroy
Instead of loading it from the SD Card directly, why not move the image to the cache in the phone's internal storage using getCacheDir() or use a temp directory to store the images in?
See this, this on external memory usage. Also, this article may be of relevance to you.
Use the below code and you will never get the following error: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inSampleSize = 4;
myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), bounds);
picturesView.setImageBitmap(myBitmap);
The best solution i found and edited according to my need
public static Bitmap getImageBitmap(String path) throws IOException{
// Allocate files and objects outside of timingoops
File file = new File(thumbpath);
RandomAccessFile in = new RandomAccessFile(file, "rws");
final FileChannel channel = in.getChannel();
final int fileSize = (int)channel.size();
final byte[] testBytes = new byte[fileSize];
final ByteBuffer buff = ByteBuffer.allocate(fileSize);
final byte[] buffArray = buff.array();
#SuppressWarnings("unused")
final int buffBase = buff.arrayOffset();
// Read from channel into buffer, and batch read from buffer to byte array;
long time1 = System.currentTimeMillis();
channel.position(0);
channel.read(buff);
buff.flip();
buff.get(testBytes);
long time1 = System.currentTimeMillis();
Bitmap bmp = Bitmap_process(buffArray);
long time2 = System.currentTimeMillis();
System.out.println("Time taken to load: " + (time2 - time1) + "ms");
return bmp;
}
public static Bitmap Bitmap_process(byte[] buffArray){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inDither=false; //Disable Dithering mode
options.inPurgeable=true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared
options.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
options.inTempStorage=new byte[32 * 1024]; //Allocate some temporal memory for decoding
options.inSampleSize=1;
Bitmap imageBitmap = BitmapFactory.decodeByteArray(buffArray, 0, buffArray.length, options);
return imageBitmap;
}
Thanks to all the threads, I've found a solution that works for me on a real device.
The tricks are all about using
BitmapFactory.Options opts=new BitmapFactory.Options();
opts.inSampleSize=(int)(target_size/bitmap_size); //if original bitmap is bigger
But for me this was not enough. My original image (taken from the Camera app) was 3264x2448. The correct ratio for me was 3, since i wanted a plain VGA image of 1024x768.
But setting inSampleSize to 3 was not enough: still out of memory exception.
So in the end I opted for a iterative approach: I start from the computed correct size, and increase it until I stop having a OOM exception.
For me it was at sample of 4.
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
// o2.inSampleSize = scale;
float trueScale = o.outWidth / 1024;
o2.inPurgeable = true;
o2.inDither = false;
Bitmap b = null;
do {
o2.inSampleSize = (int) trueScale;
Log.d(TAG, "Scale is " + trueScale);
try {
b = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (OutOfMemoryError e) {
Log.e(TAG,"Error decoding image at sampling "+trueScale+", resampling.."+e);
System.gc();
try {
Thread.sleep(50);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
trueScale += 1;
} while (b==null && trueScale < 10);
return b;
You must not depends on the GC to recycle your bitmap memory.
You must clearly recycle the bitmap when it is not needed.
See the Bitmap method:
void recycle()
Free up the memory associated with this bitmap's pixels, and mark the bitmap as "dead", meaning it will throw an exception if getPixels() or setPixels() is called, and will draw nothing.
Try this another way...
Bitmap bmpOrignal = BitmapFactory.decodeFile("/sdcard/mydata/" + path");
Allows inSampleSize resize the final read image.
getLength() of AssetFileDescriptor allows get size of file.
You can vary inSampleSize according to getLength() to prevent OutOfMemory like this :
private final int MAX_SIZE = 500000;
public Bitmap readBitmap(Uri selectedImage)
{
Bitmap bm = null;
AssetFileDescriptor fileDescriptor = null;
try
{
fileDescriptor = this.getContentResolver().openAssetFileDescriptor(selectedImage,"r");
long size = fileDescriptor.getLength();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = (int) (size / MAX_SIZE);
bm = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try {
if(fileDescriptor != null) fileDescriptor.close();
} catch (IOException e) {}
}
return bm;
}

Categories

Resources