简体   繁体   中英

ImageView not showing image from bitmap created by PDFRenderer on Android API 29

I have a PDF that I want to show in an ImageView. It's showing on API 24-28 but no image shows on API 29. I think the issue may be the bitmap created by the PDFRenderer.

I put a breakpoint after the .render method and (in the debugger) clicked "View Bitmap" here: 点击“查看位图

On API 29, I sometimes get the image, and sometimes I get this error:

Error while evaluating expression, Object has been collected

位图预览

On API 24-28, I do not get this error. I always get the image.

Below is the sample code:

        File file = new File(getExternalFilesDir(null), "fileName.pdf");
        PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY));
        if (currentPage < 0) {
            currentPage = 0;
        } else if (currentPage > renderer.getPageCount()) {
            currentPage = renderer.getPageCount() - 1;
        }
        Bitmap bitmap = Bitmap.createBitmap(800, 1000, Bitmap.Config.ARGB_8888);
        Rect rect = new Rect(0, 0, 800, 1000);
        PdfRenderer.Page page = renderer.openPage(currentPage);
        page.render(bitmap, rect, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
        page.close();
        renderer.close();
        image.setImageBitmap(bitmap);
        image.invalidate();

Does anyone know why the image is always showing on API 24-28 but not on API 29?

I ran into this on API 30 also. It looks garbage collection may be more aggressive now for bitmaps. I solved this by changing the scope of the problem bitmap from private to public and setting it to null when the relevant view is destroyed.

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