如何捕获RuntimeException

时间:2018-09-07 23:09:58

标签: android try-catch

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

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

我的问题是我无法使用以下尝试捕获来捕获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。那是怎么回事?

3 个答案:

答案 0 :(得分:1)

我相信是因为这个原因。

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

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

您真的应该在设置位图之前先缩放位图,而不要依赖它会引发异常的事实。

答案 1 :(得分:0)

我认为您不是此代码抛出的catching exceptionGlideBitmapFactory.decodeFile(photoPath)。它正在抛出runtime exception。建议将OutOfMemoryError替换为runtime exception。使用decodeFile(photoPath, 100, 100)对需要第二个和第三个参数widthheight的位图进行下采样。<​​/ p>

答案 2 :(得分:-1)

您的异常中没有任何代码,这可能是可能的原因。

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