导出多个图像或将图像导出为PDF会导致OutOfMemoryException

时间:2014-03-24 18:18:20

标签: android pdf bitmap out-of-memory itextpdf

我不知道这个例外是什么,因为我尝试丢弃每个位图,因为它们已被使用并将流刷新到我写这些图像的位置。我使用这种方法将位图保存为PDF,但这个想法可能只是连续导出一些位图。

我使用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();

0 个答案:

没有答案