位图转换为 webp 给出信号 11

bitmap conversion to webp gives signal 11

我的 Android 应用程序在我将位图转换为 WEBP 格式时出现此错误:

  #00  pc 00000000004125c4  /system/lib/libskia.so
  #01  pc 000000000042e350  /system/lib/libskia.so
  #02  pc 000000000042db78  /system/lib/libskia.so
  #03  pc 000000000042dc60  /system/lib/libskia.so (WebPPictureImportRGBA+36)
  #04  pc 00000000003a1fd0  /system/lib/libskia.so (_ZN18SkWEBPImageEncoder8onEncodeEP9SkWStreamRK8SkBitmapi+444)
  #05  pc 00000000000a216f  /system/lib/libandroid_runtime.so
  #06  pc 0000000001aabea3  /system/framework/arm/boot-framework.oat (android.graphics.Bitmap.nativeCompress+158)
  #07  pc 0000000001aad39b  /system/framework/arm/boot-framework.oat (android.graphics.Bitmap.compress+230)
  #08  pc 00000000000a9e41  /system/lib/libart.so (art_quick_invoke_stub_internal+64)
  #09  pc 00000000004087e1  /system/lib/libart.so (art_quick_invoke_stub+232)
  #10  pc 00000000000b1115  /system/lib/libart.so (_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+136)
  #11  pc 00000000001ef3fd  /system/lib/libart.so (_ZN3art11interpreter34ArtInterpreterToCompiledCodeBridgeEPNS_6ThreadEPNS_9ArtMethodEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+200)
  #12  pc 00000000001e99ad  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+492)
  #13  pc 0000000000402bb3  /system/lib/libart.so (MterpInvokeVirtualQuick+322)
  #14  pc 00000000000a0a14  /system/lib/libart.so (ExecuteMterpImpl+29972)
  #15  pc 00000000001cc463  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+290)
  #16  pc 00000000001d0fcb  /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
  #17  pc 00000000001e9993  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+466)
  #18  pc 0000000000402bb3  /system/lib/libart.so (MterpInvokeVirtualQuick+322)
  #19  pc 00000000000a0a14  /system/lib/libart.so (ExecuteMterpImpl+29972)
  #20  pc 00000000001cc463  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+290)
  #21  pc 00000000001d0fcb  /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
  #22  pc 00000000001e9993  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+466)
  #23  pc 0000000000402bb3  /system/lib/libart.so (MterpInvokeVirtualQuick+322)
  #24  pc 00000000000a0a14  /system/lib/libart.so (ExecuteMterpImpl+29972)
  #25  pc 00000000001cc463  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+290)
  #26  pc 00000000001d0f35  /system/lib/libart.so (_ZN3art11interpreter30EnterInterpreterFromEntryPointEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameE+92)
  #27  pc 00000000003f7bb3  /system/lib/libart.so (artQuickToInterpreterBridge+706)
  #28  pc 00000000000ae893  /system/lib/libart.so (art_quick_to_interpreter_bridge+34)
  #29  pc 000000000208fc09  /system/framework/arm/boot-framework.oat (android.view.View.performClick+276)
  #30  pc 0000000002109f95  /system/framework/arm/boot-framework.oat (android.view.View$PerformClick.run+40)
  #31  pc 000000000182fbe5  /system/framework/arm/boot-framework.oat (android.os.Handler.handleCallback+48)
  #32  pc 000000000182fc43  /system/framework/arm/boot-framework.oat (android.os.Handler.dispatchMessage+54)
  #33  pc 0000000001e24e39  /system/framework/arm/boot-framework.oat (android.os.Looper.loop+1948)
  #34  pc 00000000018445f3  /system/framework/arm/boot-framework.oat (android.app.ActivityThread.main+814)
  #35  pc 00000000000a9e41  /system/lib/libart.so (art_quick_invoke_stub_internal+64)
  #36  pc 00000000004088e9  /system/lib/libart.so (art_quick_invoke_static_stub+228)
  #37  pc 00000000000b113f  /system/lib/libart.so (_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+178)
  #38  pc 0000000000318d21  /system/lib/libart.so (_ZN3artL18InvokeWithArgArrayERKNS_33ScopedObjectAccessAlreadyRunnableEPNS_9ArtMethodEPNS_8ArgArrayEPNS_6JValueEPKc+56)
  #39  pc 000000000031a0b3  /system/lib/libart.so (_ZN3art12InvokeMethodERKNS_33ScopedObjectAccessAlreadyRunnableEP8_jobjectS4_S4_j+766)
  #40  pc 00000000002c12d5  /system/lib/libart.so (_ZN3artL13Method_invokeEP7_JNIEnvP8_jobjectS3_S3_+40)
  #41  pc 00000000005ae931  /system/framework/arm/boot-core-oj.oat (java.lang.reflect.Method.invoke+116)
  #42  pc 000000000236cf89  /system/framework/arm/boot-framework.oat (com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run+100)
  #43  pc 000000000236e3df  /system/framework/arm/boot-framework.oat (com.android.internal.os.ZygoteInit.main+1978)
  #44  pc 00000000000a9e41  /system/lib/libart.so (art_quick_invoke_stub_internal+64)
  #45  pc 00000000004088e9  /system/lib/libart.so (art_quick_invoke_static_stub+228)
  #46  pc 00000000000b113f  /system/lib/libart.so (_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+178)
  #47  pc 0000000000318d21  /system/lib/libart.so (_ZN3artL18InvokeWithArgArrayERKNS_33ScopedObjectAccessAlreadyRunnableEPNS_9ArtMethodEPNS_8ArgArrayEPNS_6JValueEPKc+56)
  #48  pc 0000000000318b33  /system/lib/libart.so (_ZN3art17InvokeWithVarArgsERKNS_33ScopedObjectAccessAlreadyRunnableEP8_jobjectP10_jmethodIDSt9__va_list+266)
  #49  pc 000000000027b61f  /system/lib/libart.so (_ZN3art3JNI21CallStaticVoidMethodVEP7_JNIEnvP7_jclassP10_jmethodIDSt9__va_list+426)
  #50  pc 000000000006837d  /system/lib/libandroid_runtime.so
  #51  pc 0000000000069c63  /system/lib/libandroid_runtime.so (_ZN7android14AndroidRuntime5startEPKcRKNS_6VectorINS_7String8EEEb+498)
  #52  pc 000000000000199f  /system/bin/app_process32
  #53  pc 0000000000016d8d  /system/lib/libc.so (__libc_init+48)
  #54  pc 00000000000015bc  /system/bin/app_process32

我不知道那个错误发生的确切位置,但我的猜测是当我在这里尝试将位图压缩为 WEBP 格式时:

public String saveImage(String id, Bitmap bitmap) {
    if(bitmap != null) {
        File folder = new File(getFilesDir() + "/packs", id);
        if (!folder.exists())
            folder.mkdirs();
        String stickerName = "sticker" + String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis())) + ".webp";
        File filename = new File(folder, stickerName);
        try {
            if (!filename.exists())
                filename.createNewFile();
            FileOutputStream out = new FileOutputStream(filename);
            bitmap.compress(Bitmap.CompressFormat.WEBP, 100, out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stickerName;
    } else {
        return null;
    }
}

位图是在我的自定义视图中创建的,然后保存为 WEBP,但每次尝试时它都会给我一个错误。正如我所说,我不知道如何调试此错误消息,所以如果我完全错了,请告诉我。

提前致谢!

所以我最终使用了 libwebp instead of Bitmap.compress(Bitmap.CompressFormat.WEBP, 100, out); and it now works fine. Here is a tutorial on how to use libwebp http://masashi-k.blogspot.com/2014/02/webp-in-android.html

编辑:它最终给了我同样的错误。我将尝试您发布的解决方案。

Android Compressor Library 添加到您的 build.gradle 并复制并粘贴此函数(如果您有 Bitmap

public static void saveBitmap(String folder, String imageName, Bitmap bitmap, Context context) throws IOException {
        File temp = new File(folder, "temp_" + imageName);

        try (OutputStream stream = new FileOutputStream(temp)) {
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        }

        new Compressor(context)
                .setQuality(90)
                .setCompressFormat(Bitmap.CompressFormat.WEBP)
                .setDestinationDirectoryPath(folder) // Saving in the same folder temp file is at
                .compressToFile(temp, imageName); // Takes the temp file and creates a new one with the name passed as second argument

        temp.delete(); // Deleting temp file

    }

如果你想知道为什么我在保存所需的 WEBP 之前保存一个临时 PNG 文件,那是因为该库没有将 Bitmap 作为参数,它仅适用于 File

库记录了一个错误 "the file should be a JPEG" 但到目前为止它工作正常,PNG alpha 通道保留在 WEBP 中所以我忽略了那个错误。