如何捕获RuntimeException

问题描述 投票:2回答:3

在我的应用程序中,我需要处理大型位图。一旦我加载我的位图我想在我的imageview中显示它

                        try {
                            bitmap = GlideBitmapFactory.decodeFile(photoPath);                              
                        } catch (OutOfMemoryError) {
                           //i do stuff in order to load bitmap                  
                        }

我的问题是我无法使用以下try catch捕获java.lang.RuntimeException

                            try {
                               ImageView i = (ImageView) findViewById(R.id.mainImageView);

                               i.setImageBitmap(bitmap);

                            } catch (Exception e) {
                               //never came here                 
                            }

logcat的:

 java.lang.RuntimeException: Canvas: trying to draw too large(116640000bytes) bitmap.
            at android.view.DisplayListCanvas.throwIfCannotDraw(DisplayListCanvas.java:260)
            at android.graphics.Canvas.drawBitmap(Canvas.java:1415)
            at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:528)
            at android.widget.ImageView.onDraw(ImageView.java:1316)
            at android.view.View.draw(View.java:17214)
            at android.view.View.updateDisplayListIfDirty(View.java:16196)
            at android.view.View.draw(View.java:16980)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3729)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3515)
            at android.view.View.updateDisplayListIfDirty(View.java:16191)
            at android.view.View.draw(View.java:16980)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3729)
            at android.support.design.widget.CoordinatorLayout.drawChild(CoordinatorLayout.java:1254)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3515)
            at android.view.View.updateDisplayListIfDirty(View.java:16191)
            at android.view.View.draw(View.java:16980)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3729)
            at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1366)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3515)
            at android.view.View.draw(View.java:17217)
            at android.view.View.updateDisplayListIfDirty(View.java:16196)
            at android.view.View.draw(View.java:16980)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3729)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3515)
            at android.view.View.updateDisplayListIfDirty(View.java:16191)
            at android.view.View.draw(View.java:16980)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3729)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3515)
            at android.view.View.updateDisplayListIfDirty(View.java:16191)
            at android.view.View.draw(View.java:16980)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3729)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3515)
            at android.view.View.updateDisplayListIfDirty(View.java:16191)
            at android.view.View.draw(View.java:16980)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3729)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3515)
            at android.view.View.updateDisplayListIfDirty(View.java:16191)
            at android.view.View.draw(View.java:16980)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3729)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3515)
            at android.view.View.draw(View.java:17217)
            at com.android.internal.policy.DecorView.draw(DecorView.java:757)
            at android.view.View.updateDisplayListIfDirty(View.java:16196)
            at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:648)
            at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:654)
            at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:762)
            at android.view.ViewRootImpl.draw(ViewRootImpl.java:2837)
            at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2645)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2252)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1291)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6396)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:876)
            at android.view.Choreographer.doCallbacks(Choreographer.java:688)
            at android.view.Choreographer.doFrame(Choreographer.java:623)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:862)
            at android.os.Handler.handleCallback(Handler.java:754)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:163)
            at android.app.ActivityThread.main(ActivityThread.java:6228)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)

我知道Exception应该抓住RuntimeException。发生什么了?

android try-catch
3个回答
1
投票

我相信是因为这个原因。

当您调用setImageBitmap()时,Android会将该位图转换为Drawable,然后在内部调用setImageDrawable()

setImageDrawable()基本上只更新一个全局变量(mDrawable),然后使ImageView无效。这意味着您不再直接连接到设置图像的过程,因为它似乎通过Handler调用,因此您不能再捕获任何抛出的异常。

你真的应该在设置之前缩放Bitmap,而不是依赖于它将抛出异常的事实。


0
投票

我认为你不是这个代码抛出的catching exceptionGlideBitmapFactory.decodeFile(photoPath)。这是扔runtime exception。建议用OutOfMemoryError取代runtime exception。使用decodeFile(photoPath, 100, 100)对位图进行缩减采样,其中需要第二个和第三个参数widthheight


-1
投票

您可能没有任何可能原因的异常代码。

RuntimeException扩展了Exception。如果你捕获Exception,你也会捕获RuntimeException。

© www.soinside.com 2019 - 2024. All rights reserved.