2

I am parsing images from XML and show it in My Android Application. my problem is when parse more than 20 image the following error occur...

06-09 14:28:37.710: ERROR/AndroidRuntime(490): java.lang.RuntimeException: An error occured while executing doInBackground() 06-09 14:28:37.710: ERROR/AndroidRuntime(490): at android.os.AsyncTask$3.done(AsyncTask.java:200) 06-09 14:28:37.710: ERROR/AndroidRuntime(490): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 06-09 14:28:37.710: ERROR/AndroidRuntime(490): at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 06-09 14:28:37.710: ERROR/AndroidRuntime(490): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 06-09 14:28:37.710: ERROR/AndroidRuntime(490): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 06-09 14:28:37.710: ERROR/AndroidRuntime(490): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068) 06-09 14:28:37.710: ERROR/AndroidRuntime(490): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561) 06-09 14:28:37.710: ERROR/AndroidRuntime(490): at java.lang.Thread.run(Thread.java:1096) 06-09 14:28:37.710: ERROR/AndroidRuntime(490): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget 06-09 14:28:37.710: ERROR/AndroidRuntime(490): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 06-09 14:28:37.710: ERROR/AndroidRuntime(490): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:459) 06-09 14:28:37.710: ERROR/AndroidRuntime(490): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:323) 06-09 14:28:37.710: ERROR/AndroidRuntime(490): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697) 06-09 14:28:37.710: ERROR/AndroidRuntime(490): at android.graphics.drawable.Drawable.createFromStream(Drawable.java:657) 
1
  • As @Egor said, you need to post some code so we can see what's going on. When you say "parse more than 20 image" - are you "parsing" them individually? If so, it shouldn't matter how many you do as you don't need to hold the previous 19 in memory while doing so. Commented Jun 9, 2011 at 14:57

7 Answers 7

5

I had the same problem once when thumbnailing images for a file editor I made. What you want to do is shrink the images before storing them to memory, and disposing of the source images from system memory once they have been read

Here is the code I used for mine, it shrinks the images to 110x110, but you can set up your own algorithm to scale it (I believe there is actually already a function for that within the resizedbitmap class, but I havent used it)

public void setImageFromFile(String filename) { ImageView fileImage = (ImageView)findViewById(R.id.file_image_holder); Bitmap icon = BitmapFactory.decodeFile(filename); int width = icon.getWidth(); int height = icon.getHeight(); int newWidth = 110; int newHeight = 110; float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; Matrix bMatrix = new Matrix(); bMatrix.postScale(scaleWidth, scaleHeight); Bitmap resizedBitmap = Bitmap.createBitmap(icon, 0, 0, width, height, bMatrix, true); BitmapDrawable bmd = new BitmapDrawable(resizedBitmap); fileImage.setImageDrawable(bmd); } 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Dear this post is very helpful to me.i ask another thing how to filter some folder from sdcard when we get the directory structure of sdcard
I dont understand what you are asking. Please clarify
I got this answer already i want to doesn`t display system folder from directory structure so i do it very well.Thank for reaply
2

Generally, there is only one solution: use smaller images. If there's no code it's hard to say what's exactly wrong. You may try to lower image size.

Comments

1

When you read in the Bitmap through the BitmapFactory, set the BitmapFactory.Option inScaled parameter to 2 or 4 (or whatever works for you) to create a smaller, less memory intensive image.

Comments

0

I had the same problem. Try to reuse the bitmaps and the canvas.

Comments

0

A lot of devices have very limited and restricted memory available to each device. A lot of older devices allocate 16MB of memory to your application, while some more modern devices might allocate 24MB or even 32MB (I've seen a device with only 14MB).

Whenever you load so many images into memory, this space fills up very quickly causing the OutOfMemoryError to be thrown. To overcome this problem, you should (as previous answers have said) shrink the images to thumbnails and only keep the thumbnails in device memory (reduced memory footprint) and have the images saved to SDCARD.

You can see how much memory your program is using from the DDMS layout in Eclipse. I believe you can also override the Application class to listen for a "Low Memory" warning function.

Comments

0

1) To Solve this problem add android:installLocation="preferExternal" in manifest file.

2) Use Compressed images .

Comments

0

If your app works fine sometimes and all of sudden you get this crashes with low memory, then probably you might be surfing through activities while creating bitmaps in them.

You might want to try this

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.