0

I have no clue what to about this exception, since I try to discard each bitmap as they've been used and flush the stream to where I write these images. I use this method to save bitmaps to a PDF, but the idea is probably the same as just exporting a number of Bitmaps in a row.

I use itextpdf (http://mvnrepository.com/artifact/com.itextpdf/itextpdf)

// List of pages containing draw objects that can be converted to bitmaps
ArrayList<Page> pages;

Rectangle pageSize = new Rectangle(width, height);
Document document = new Document(pageSize, 0, 0, 0, 0);

PdfWriter.getInstance(document, new FileOutputStream(saveDirectory));

document.open();

ByteArrayOutputStream stream = new ByteArrayOutputStream();

for (int i=0; i < pages; i++)
{

    // From a list of objects, draws them on a Bitmap canvas and returns the bitmap.
    Bitmap b = DrawEngine.Draw(width, height, originalWidth, originalHeight, pages.get(i).GetDrawObjects());

    stream.flush();
    stream.reset();

    b.compress(CompressFormat.PNG, 100, stream);
    b.recycle();

    byte[] byteImage = stream.toByteArray();
    Image image = Image.getInstance(byteImage);
    document.add(image);

    // Update notification

    System.gc();

}

document.close();
Deukalion
  • 2,516
  • 9
  • 32
  • 50
  • Exporting at 72 DPI works fine, at 300 DPI and after 4 bitmaps a OutOfMemoryException is thrown... – Deukalion Mar 24 '14 at 18:29
  • How much memory do you allot to your program? – mkl Mar 24 '14 at 20:51
  • No idea. I've removed the itextpdf features, just drawing all the bitmaps - then it's no problem. Problem is to add all the images to the document, which is what's causing the memory problem. If I just render all images, not attaching them to anything it works. – Deukalion Mar 24 '14 at 21:01
  • *No idea* - but the first thing to do in an OutOfMemory situation is to analyze how much memory what parts of your program use, how much you do provide and how much you can provide, isn't it? – mkl Mar 25 '14 at 06:15
  • What I've tried is figuring out if it's the creation of unlimited bitmaps that causes the problem or if it's the PDF creation. Since only rendering multiple bitmaps no longer causes the OutOfMemory exception, at least I've pinpointed that error. Since that's the only place where it occurs. – Deukalion Mar 25 '14 at 12:32
  • I added android:largeHeap="true" to my Manifest, and while exporting I write out the Debug.getNativeSize() and Debug.getNativeHeapFreeSize() and it returns ~29 and ~0.32, after being divided by 1048576.0. – Deukalion Mar 25 '14 at 12:59
  • After adding largeHeap="true" it no longer crashes, but chances are it could happen again. – Deukalion Mar 25 '14 at 13:01
  • You have to keep in mind that any standard image format with the exception of jpeg and a certain profile of jpeg2000 is foreign to PDF and, therefore, has to be converted into PDF's internal bitmap format. During this transformation you'll have both the original `byte[]` you provide and some internal, converted structures in memory. Thus, memory consumption rises quite a bit. – mkl Mar 25 '14 at 13:09
  • So instead of using PNG you suggest I use JPEG as the format to add? – Deukalion Mar 25 '14 at 13:57
  • *you suggest* - No, I merely explained why you should expect increased resource requirements in your use case. Whether jpeg or png is better depends on the drawn objects. Actually reading your comment `From a list of objects, draws them on a Bitmap canvas and returns the bitmap` I wonder whether you should draw using PDF vector graphics instead of drawing a bitmap. That would give you arbitrary resolution for free... – mkl Mar 25 '14 at 19:24
  • I'm not sure if it's possible using itextpf, but it might be. I haven't thought about it. – Deukalion Mar 26 '14 at 12:45
  • *I'm not sure if it's possible using itextpf* - Have a look at the methods provided by `PdfContentByte`. Instances of that class are provided e.g. as `PdfWriter.DirectContent` or `PdfWriter.DirectContentUnder`. – mkl Mar 26 '14 at 14:17
  • Trying now, but scaling is a problem and some methods need to get rewritten entirely to be able to Paint out the graphics. I can't find a way to scale the entire content down, because it should be able to render in different resolution perfectly. For example java.awt.AffineTransform is not supported by Android, and I can therefor not use the .transform() method. – Deukalion Mar 26 '14 at 14:27
  • Any other OpenSource PDF Creation projects that support the use of Android's Canvas? That would be a lot easier. All I found is Qoppa, but that's a commerical one. – Deukalion Mar 26 '14 at 14:30

0 Answers0