抛出OutOfMemoryError异常

时间:2017-12-29 11:44:35

标签: java android bitmap out-of-memory pdf-reader

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

  

这里是一个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)
  

这是我的AndroidManifest声明

   <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">
  

这是我创建位图的代码

 @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;
    }
}

1 个答案:

答案 0 :(得分:4)

        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像素 - 你可能会遇到因释放Bitmap的政策导致的内存问题,当加上Android垃圾收集器的限制时

并且,除非通过Android API创建PDF,否则PdfRenderer不太可能正确地呈现它。该类存在用于打印预览,而不是用于呈现任意PDF内容。

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