gpt4 book ai didi

java - 抛出 OutOfMemoryError 异常

转载 作者:行者123 更新时间:2023-12-01 17:52:55 27 4
gpt4 key购买 nike

在我的应用程序中,我想以位图形式呈现 PDF 文件。我在 StackOverflow 上找到了很多解决方案,但到目前为止没有任何效果。我进行了以下更改,android:largeHeap="true"android:hardwareAccelerated="false"

Here a stacktrace

FATAL EXCEPTION: main
Process: com.myclasscampus.aspire, PID: 6214
java.lang.OutOfMemoryError: Failed to allocate a 434035012 byte allocation with 4194304 free bytes and 328MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:879)
at android.graphics.Bitmap.createBitmap(Bitmap.java:856)
at android.graphics.Bitmap.createBitmap(Bitmap.java:823)
at com.aspire.pdfViewer.PDFViewFragment.showPage(PDFViewFragment.java:176)
at com.aspire.pdfViewer.PDFViewFragment.openRenderer(PDFViewFragment.java:154)
at com.aspire.pdfViewer.PDFViewFragment.onCreateView(PDFViewFragment.java:86)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
at android.app.Activity.performStart(Activity.java:6679)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

Here is my AndroidManifest declaration

   <application
android:name="com.aspire.webserviceConstant.AppController"
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme.calender"
tools:replace="android:name">

Here is my code which creates bitmap

 @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void showPage(int index) {
try {

if (pdfRenderer.getPageCount() <= index) {
return;
}

if (null != currentPage) {
currentPage.close();
}

currentPage = pdfRenderer.openPage(index);
releaseBitmap();

bitmapPDF = Bitmap.createBitmap(
getResources().getDisplayMetrics().densityDpi * currentPage.getWidth() / 72,
getResources().getDisplayMetrics().densityDpi * currentPage.getHeight() / 72,
Bitmap.Config.ARGB_8888);

currentPage.render(bitmapPDF, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
imgPdfView.setImageBitmap(bitmapPDF);
updateUIData();

} catch (Exception e) {
e.printStackTrace();
}
}

private void releaseBitmap() {
if (bitmapPDF != null) {
bitmapPDF.recycle();
bitmapPDF = null;
}
}

最佳答案

        bitmapPDF = Bitmap.createBitmap(
getResources().getDisplayMetrics().densityDpi * currentPage.getWidth() / 72,
getResources().getDisplayMetrics().densityDpi * currentPage.getHeight() / 72,
Bitmap.Config.ARGB_8888);

这行代码尝试分配 434,035,012 字节的内存。对于 ARGB_8888,每像素 4 字节,相当于 10,416 x 10,416 像素图像。

目前还没有常见的 Android 设备的屏幕分辨率接近该尺寸。

即使您将其减少相当多(例如,减少到大约 1000 x 1000 像素),由于您释放位图的策略,再加上以下限制,您也可能会遇到内存问题: Android 的垃圾收集器。

并且,除非通过 Android API 创建 PDF,否则 PdfRenderer 无论如何都不太可能正确呈现它。该类的存在是为了打印预览,而不是为了呈现任意 PDF 内容。

如果您尝试渲染任意 PDF,请使用 other PDF rendering options 。如果您尝试对通过 Android 绘图代码创建的 PDF 进行打印预览,并且希望坚持使用 PdfRenderer,请降低分辨率并使用尽可能少的 Bitmap 对象。可以通过重用以前分配的页面而不是为每个页面分配新的页面来实现。

关于java - 抛出 OutOfMemoryError 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48022115/

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