简体   繁体   中英

Android itext pdf library OutOfMemoryError

I am using itext library in order to create a pdf of images and text. The following code works fine on 3 out of 4 devices but on the 4th device I get OutOfMemoryException. The code is being run inside an AsyncTask doinbackground method, it is checking the rotation of each jpg file (directory of files from sdcard) using a bimtmap object, measuring the width against the height, and rotating the itext Image object 90degrees if necessary.

        try {

            Document document = new Document();
            PdfWriter.getInstance(document, new FileOutputStream(
                    Environment.getExternalStorageDirectory()
                            + "/doc" + documentID + ".pdf"));
            document.open();
            document.setMargins(0f, 0f, 0f, 0f);
            document.add(new Paragraph(documentNameEt.getText().toString()));
            document.add(new Paragraph(makeUserDeets()));
            Bitmap b = null;
            Image image = null;
            Rectangle r = document.getPageSize();
            for (String d : paths[0]) {
                Log.d("senddataobj", d);
                image = Image.getInstance(d);
                b = BitmapFactory.decodeFile(d);
                if (b.getWidth() > b.getHeight()) {
                    image.setRotationDegrees(90);
                    Log.d(d, "rotating");
                }
                b.recycle();
                image.scaleToFit(r.getWidth(), r.getHeight());
                document.add(image);                
            }

            document.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:278) at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) at java.util.concurrent.FutureTask.setException(FutureTask.java:124) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) at java.util.concurrent.FutureTask.run(FutureTask.java:137) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) at java.lang.Thread.run(Thread.java:856) Caused by: java.lang.OutOfMemoryError at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493) at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:299) at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:324) at com .wake.snapshot.CreateDocumentActivity$CreateAndSendPdf.doInBackground(CreateDocumentActivity.java:315) at com.wake.up.your.wealth.snapshot.CreateDocumentActivity$CreateAndSendPdf.doInBackground(CreateDocumentActivity.java:1) at android.os.AsyncTask$2.call(AsyncTask.java:264) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) ... 5 more

因为只需要一个位图大小,所以将BitmapFactory与inJustDecodeBounds选项一起使用http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inJustDecodeBounds ...它将返回空位图,但是您将拥有该位图的大小in outHeight和outWidth

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