简体   繁体   中英

Android pdf writer APW high resolution images cause out of memory expection

I am using android pdf writer (apw) in my app successfully for the most part. However, when I try to include a high resolution in a pdf document, I get an out of memory exception.

Immediately before creating the pdf file, the library must have the content itself converted into a string (representing the raw pdf content), which is then converted to a byte array. The byte array is written to the file in a file output stream (see example via website).

The out of memory expection occurs when the string is generated because representing all the pixels of a bitmap image in string format is very memory intensive. I could downsample the image using the android API, however, it is essential that the images are put into the pdf at high resolution (~2000 x 1000).

There are many scanner type apps which seem to be able to able generate pdf high res images, so there must be a way around it, surely. Granted, they may be using other libraries, but surely there is someone who has figured out a way around it with this library given that it is free and therefore popular(?)

I emailed the developer, but there was no response.

Potential solutions (I can think of) include:

Modifying the library to load a string representing eg the first 10% of the PDF, and writing to file chunk by chunk. (edit)

Modifying the library to output a stringoutput stream, or other output stream to a temp file (or final file) as the actual pdf content is being written in the pdfwriter object.

However as a relative java noob (and even more of a pdf specification noob), I am unable to understand the library well enough to do this myself.

Has anyone come across this problem and found a way around it? Anyone willing to hazard a suggestion, or take a look at the library itself even to see if there is a fix of some sort.

Thanks for your help. nme32

Edit:

  1. Logcat says heap size is in the range on 40 to 60mb before the crash. I understand (do correct me if not) that Android limits the available memory to apps depending on what else is running, though it is in the 50mb ballpark, depending on device.

  2. When loading the image, I think APW essentially converts it to bitmap, that is represents the image pixel by pixel then puts it into string format, meaning it doesn't matter which image format you use, it may as well be bitmap.

First of all the resolution you are mentioning is very high. And i have already mentioned the issues related to Images in Android in this Answer

Secondly in case first solution doesn't work for you i would suggest Disk based LruCache .And store the chunks into that disk based cache and then retrieve and use it. Here is an Example of that .

Hope this would help. If it doesn't comment on this answer and i will add more solutions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM