Bitmap decodeStream OutOfMemory Exception?

Taken from here: memofy.com/memofy/show/1008ab7f2836ab7f0... Try this: BitmapFactory. Options o = new BitmapFactory.Options(); options. InTempStorage = new byte16*1024; Bitmap bitmapImage = BitmapFactory.

DecodeFile(path, options); Instead of: BitmapFactory. Options o = new BitmapFactory.Options(); final int REQUIRED_SIZE=300*1024; So, before using BitmapFactory.decodeFile() create a byte array of 16kb and pass it to temp storage in decoding process. Hope that helps!

Referenced: Android: Strange out of memory issue.

I had the same problem over and over again. Heres my code maby its alitle overkill but I had to this cause of diffrent camera size, res etc , but you`ll have to adjust it to your needs. BitmapFactory.

Options imageOptions = new BitmapFactory.Options(); imageOptions. InJustDecodeBounds = true; ByteArrayInputStream imageByteArrayInputStream = new ByteArrayInputStream(data); BitmapFactory. DecodeStream(imageByteArrayInputStream, null, imageOptions); System.gc(); // Decode frame size BitmapFactory.

Options frameOptions = new BitmapFactory.Options(); frameOptions. InJustDecodeBounds = true; BitmapFactory. DecodeResource(getResources(), selectedFrameResourceID, frameOptions); System.gc(); // Scale factor for pre scaling int preScaleFactor = 1; if (imageOptions.

OutWidth > frameOptions. OutWidth || imageOptions. OutHeight > frameOptions.

OutHeight) { preScaleFactor = Math. Max(imageOptions. OutWidth / frameOptions.

OutWidth, imageOptions. OutHeight / frameOptions. OutHeight); } // Decode with inSampleSize BitmapFactory.

Options scaleOptions = new BitmapFactory.Options(); scaleOptions. InSampleSize = preScaleFactor; imageByteArrayInputStream = new ByteArrayInputStream(data); Bitmap preScaledBitmap = BitmapFactory. DecodeStream(imageByteArrayInputStream, null, scaleOptions); System.gc(); Bitmap finalBitmap; // Scale factor for precise scaling // If the scaled image is not exactly the same size as the frame than resize it precisely if (preScaledBitmap.getWidth()!

= frameOptions. OutWidth || preScaledBitmap.getHeight()! = frameOptions.

OutHeight) { float scaleFactor = Math. Max((float)((float)preScaledBitmap.getWidth() / (float)frameOptions. OutWidth), (float)((float)preScaledBitmap.getHeight() / (float)frameOptions.

OutHeight)); float scalePercentage = Math. Min((float)((float)frameOptions. OutWidth / (float)preScaledBitmap.getWidth()), (float)((float)frameOptions.

OutHeight / (float)preScaledBitmap.getHeight())); Matrix matrix = new Matrix(); matrix. PreScale(scalePercentage, scalePercentage); // If the capture width for the source is bigger than the actual width of the source, then set is to the max of the actual source width int sourceCaptureWidth = (int)(frameOptions. OutWidth * scaleFactor); if (sourceCaptureWidth > preScaledBitmap.getWidth()) { sourceCaptureWidth = preScaledBitmap.getWidth(); } // Same as above but than for the height int sourceCaptureHeight = (int)(frameOptions.

OutHeight * scaleFactor); if (sourceCaptureHeight > preScaledBitmap.getHeight()) { sourceCaptureHeight = preScaledBitmap.getHeight(); } finalBitmap = Bitmap. CreateBitmap(preScaledBitmap, 0, 0, sourceCaptureWidth, sourceCaptureHeight, matrix, true); preScaledBitmap.recycle(); preScaledBitmap = null; Hope this helps.

Actually I implement lazy loading in my getView function and it's working without any errors for now. I guess the whole problem was in that I'm decoding stream in getView function where it's allocating too much memory. Thanks anyway for the answer!

If it's still giving me an error I'll try your solution. – Android-Droid Jan 8 at 21:19 No problem , I'm glad you figured it out. – Maikel Bollemeijer Jan 8 at 22:07.

The REQUIRED_SIZE should contain the max dimension (width, height in pixels) like final int REQUIRED_SIZE = 1024; // 1024 pixels wide or long. You also missed couple of lines for getting image bounds into BitmapFactory. Options o before calculating the scaling factor.

//Decode image size BitmapFactory. Options o = new BitmapFactory.Options(); o. InJustDecodeBounds = true; BitmapFactory.

DecodeStream(cis, null, o); //The new size we want to scale to final int REQUIRED_SIZE = 1024; Then use o. OutWidth and o. OutHeight for calculating the scale factor.

You might need to fetch cis again for the actual decoding of the stream. Update: Also, You can make the following variables as members of adapter and initialize in the constructor. SecretKeySpec keySpec = new SecretKeySpec("01234567890abcde".getBytes(), "AES"); IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes()); This should work with no issues.

Use recycle(). It will free the native object associated with this bitmap, and clear the reference to the pixel data. Bitmap ops = BitmapFactory.

DecodeStream(cis,null,o2); ((ImageView) convertView. FindViewById(R.id. ImgView)).

SetImageBitmap(ops); cis.close(); fis.close(); ops.recycle(); System.gc().

While I try to use recycle it's throwing me this exception : java.lang. RuntimeException: Canvas: trying to use a recycled bitmap android.graphics. Bitmap@40627090, 640x960 – Android-Droid Dec 24 '11 at 11:24 you can use recycle() in finally block.

Try to do this. – Sujit Dec 24 '11 at 11:55 still got the same problem. – Android-Droid Dec 24 '11 at 12:34.

If you run it on Honeycomb or higher, you should be able to use the Eclipse memory analyzer to see where your memory is getting used. That being said, you need to call recycle() on your bitmaps as soon as they are no longer needed or displayed (which is the problem with Sujits answer). In other words, if a Bitmap goes off the screen it would be best to recycle() it and then re-load it again when it comes back into view.

Otherwise, that Bitmap is usi To do this, call getDrawable() on your ImageView, call setImageDrawable(null) on your ImageView, and then cast the drawable to a BitmapDrawable and recycle the bitmap inside of it. For more information about how bitmap memory worked pre Android 3.0, you can see a post I did on this issue: http://code.google.com/p/android/issues/detail?id=8488#c80.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions