gpt4 book ai didi

java - Android itext pdf库OutOfMemoryError

转载 作者:行者123 更新时间:2023-12-01 14:18:54 26 4
gpt4 key购买 nike

我正在使用 itext 库来创建图像和文本的 pdf。以下代码在四分之三的设备上运行良好,但在第四台设备上我收到 OutOfMemoryException。该代码在 AsyncTask doinbackground 方法内运行,它使用 bimtmap 对象检查每个 jpg 文件(sdcard 中的文件目录)的旋转,测量宽度与高度,并在必要时将 itext Image 对象旋转 90 度。

        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 ...它将返回空位图,但您将在 outHeight 和 outWidth 中获得大小

关于java - Android itext pdf库OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17823195/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com