I have an map app. Somewhere in my code I am rendering data from database into canvas.
I ran into “out of memory” exception and I cant figure out how to avoid it.
Here is the relevant method. I get exception when creating bitmap with bitmapfactory.
private static void _renderImage(Canvas g, Point[] points, RImageData imageData,
RMapView mapView) {
Bitmap image = (Bitmap)imageData.image;
Paint paint = new Paint();
if(image == null) {
image = BitmapFactory.decodeByteArray(imageData.getImageBytes(), 0,
imageData.getImageBytes().length);
imageData.image = image;
}
g.drawBitmap(image, points[0].x, points[0].y, paint);
}
I have tried recycling the image, but then the canvas coplains it cant work with recycled bitmaps.
Any solution would be much apreciated.
I would suggest having a bitmap cache. Even recycling images on pre-honeycomb takes time to free memory (Bitmap data is stored in native memory that is not directly managed by dalvik). Here is a sample of a bitmap cache. Please adjust it to your needs.