【问题标题】:Can't catch Java (Android) Exception with try-catch , createBitmap无法使用 try-catch、createBitmap 捕获 Java (Android) 异常
【发布时间】:2015-08-11 20:53:30
【问题描述】:

我有一段代码,我用了四次这个代码,但是第六次,我的应用程序崩溃了,我没有得到任何异常或错误代码。代码是:

Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

我尝试使用:

try{

Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

    }catch (Exception e){
        e.printStackTrace();
        e.getMessage();
    }catch (OutOfMemoryError o){
        o.printStackTrace();
        o.getMessage();
    }catch (AssertionError a){
        a.printStackTrace();
        a.getMessage();
    } catch ( Throwable t ){
        t.printStackTrace();
        t.getMessage();
    }

但是什么也没发生。我在不同的配置中测试我的应用程序,在不同模拟器的 Sony Xperia Z3、Moto G、GenyMotion 中运行良好。

但不适用于我的摩托罗拉 Moto Max。

日志控制台不显示错误,但打印一些代码:

  --------- beginning of main
08-11 15:34:55.589    6258-6258/? E/Diag_Lib﹕ Diag_LSM_Init: Failed to open handle to diag driver, error = 2
08-11 15:35:00.638    6275-6275/? E/Diag_Lib﹕ Diag_LSM_Init: Failed to open handle to diag driver, error = 2
    --------- beginning of system
08-11 15:35:00.723     817-1225/? V/AlarmManager﹕ send {ea59bfe, *alarm*:android.intent.action.TIME_TICK}
08-11 15:35:00.723     817-1225/? V/AlarmManager﹕ send {26ca6698, *alarm*:com.google.android.gms/.lockbox.LockboxAlarmReceiver}
08-11 15:35:00.748      817-817/? V/AlarmManager﹕ done {26ca6698, *alarm*:com.google.android.gms/.lockbox.LockboxAlarmReceiver} [26ms]
    --------- beginning of crash
08-11 15:35:00.937    5910-5951/garagecoders.tecnoblinds.main A/libc﹕ Fatal signal 11 (SIGSEGV), code 2, fault addr 0xaf1f9000 in tid 5951 (AsyncTask #1)
08-11 15:35:00.946      817-817/? V/AlarmManager﹕ done {ea59bfe, *alarm*:android.intent.action.TIME_TICK} [224ms]
08-11 15:35:01.039      348-348/? I/DEBUG﹕ *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
08-11 15:35:01.039      348-348/? I/DEBUG﹕ Build fingerprint: 'motorola/quark_retla/quark_umts:5.0.2/LXG22.33-12.16/16:user/release-keys'
08-11 15:35:01.039      348-348/? I/DEBUG﹕ Revision: 'p4a0'
08-11 15:35:01.039      348-348/? I/DEBUG﹕ ABI: 'arm'
08-11 15:35:01.040      348-348/? I/DEBUG﹕ pid: 5910, tid: 5951, name: AsyncTask #1  >>> garagecoders.tecnoblinds.main <<<
08-11 15:35:01.040      348-348/? I/DEBUG﹕ signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0xaf1f9000
08-11 15:35:01.068      348-348/? I/DEBUG﹕ r0 00000004  r1 af1f8ffc  r2 024760c6  r3 f5d0f000
08-11 15:35:01.068      348-348/? I/DEBUG﹕ r4 b4873340  r5 000061cd  r6 00000247  r7 000002fb
08-11 15:35:01.068      348-348/? I/DEBUG﹕ r8 00000248  r9 00004000  sl 00000000  fp 00003ff0
08-11 15:35:01.069      348-348/? I/DEBUG﹕ ip af1f9000  sp a0f3d078  lr 00000247  pc b5af8014  cpsr 200b0010
08-11 15:35:01.069      348-348/? I/DEBUG﹕ backtrace:
08-11 15:35:01.069      348-348/? I/DEBUG﹕ #00 pc 001bd014  /system/lib/libskia.so (NofilterDXExecutionBuffer::GenerateUpscaleProgram(int, int, int, int)+128)
08-11 15:35:01.069      348-348/? I/DEBUG﹕ #01 pc 001bdcfc  /system/lib/libskia.so (NofilterDXExecutionBuffer::GenerateProgram(int, int, int, int)+92)
08-11 15:35:01.070      348-348/? I/DEBUG﹕ #02 pc 001bddf4  /system/lib/libskia.so (NfDXEBCache::prepareShaderProc32(int, int, int, int)+232)
08-11 15:35:01.070      348-348/? I/DEBUG﹕ #03 pc 001ba4d8  /system/lib/libskia.so (SkBitmapProcState::beginRect(int, int, int)+424)
08-11 15:35:01.070      348-348/? I/DEBUG﹕ #04 pc 001bade8  /system/lib/libskia.so (SkARGB32_Shader_Blitter::blitRect(int, int, int, int)+1244)
08-11 15:35:01.070      348-348/? I/DEBUG﹕ #05 pc 0010616b  /system/lib/libskia.so
08-11 15:35:01.070      348-348/? I/DEBUG﹕ #06 pc 00106237  /system/lib/libskia.so (SkScan::FillIRect(SkIRect const&, SkRegion const*, SkBlitter*)+198)
08-11 15:35:01.070      348-348/? I/DEBUG﹕ #07 pc 00106307  /system/lib/libskia.so (SkScan::FillRect(SkRect const&, SkRegion const*, SkBlitter*)+136)
08-11 15:35:01.070      348-348/? I/DEBUG﹕ #08 pc 001063f7  /system/lib/libskia.so (SkScan::FillRect(SkRect const&, SkRasterClip const&, SkBlitter*)+56)
08-11 15:35:01.071      348-348/? I/DEBUG﹕ #09 pc 000e2037  /system/lib/libskia.so (SkDraw::drawRect(SkRect const&, SkPaint const&) const+258)
08-11 15:35:01.071      348-348/? I/DEBUG﹕ #10 pc 000e3513  /system/lib/libskia.so (SkDraw::drawBitmap(SkBitmap const&, SkMatrix const&, SkPaint const&) const+276)
08-11 15:35:01.071      348-348/? I/DEBUG﹕ #11 pc 000cb863  /system/lib/libskia.so (SkBitmapDevice::drawBitmapRect(SkDraw const&, SkBitmap const&, SkRect const*, SkRect const&, SkPaint const&, SkCanvas::DrawBitmapRectFlags)+530)
08-11 15:35:01.071      348-348/? I/DEBUG﹕ #12 pc 000db98f  /system/lib/libskia.so (SkCanvas::internalDrawBitmapRect(SkBitmap const&, SkRect const*, SkRect const&, SkPaint const*, SkCanvas::DrawBitmapRectFlags)+210)
08-11 15:35:01.071      348-348/? I/DEBUG﹕ #13 pc 00093661  /system/lib/libandroid_runtime.so (android::SkiaCanvas::drawBitmap(SkBitmap const&, float, float, float, float, float, float, float, float, SkPaint const*)+58)
08-11 15:35:01.071      348-348/? I/DEBUG﹕ #14 pc 0008945d  /system/lib/libandroid_runtime.so
08-11 15:35:01.071      348-348/? I/DEBUG﹕ #15 pc 0089eeff  /system/framework/arm/boot.oat

有什么建议吗?

【问题讨论】:

    标签: android exception android-bitmap


    【解决方案1】:

    我很确定您的设备内存不足,这就是第 6 次 createBitmap 会使您的应用程序崩溃的原因。请尝试以下操作:

    try{
        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
    } catch (Exception e) {
        e.printStackTrace();
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
    }
    

    这应该正确地为抛出的错误提供输出。让我知道输出,您可能需要手动调用 System.gc() 来自己处理所有垃圾收集。

    阅读:Displaying Bitmaps Efficiently

    【讨论】:

    • OutOfMemoryError 和 throwable 的异常扩展,必须由 }catch(Throwable e) { } 处理,我对 Moto G .... 和 Moto MAx 等其他处置没有问题,有更多的内存...
    猜你喜欢
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 2017-08-07
    • 2014-03-22
    • 1970-01-01
    • 2015-06-21
    相关资源
    最近更新 更多