gpt4 book ai didi

android - 如何捕获运行时异常

转载 作者:行者123 更新时间:2023-11-29 00:59:32 34 4
gpt4 key购买 nike

在我的应用程序中,我需要处理大型位图。加载位图后,我想在 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
}

日志:

 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。那么这是怎么回事?

最佳答案

我相信是因为这个。

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

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

您真的应该在设置位图之前缩放位图,而不是依赖它会抛出异常这一事实。

关于android - 如何捕获运行时异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52230608/

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