3

I am trying to compress And Decode the Image to base64 string using the following method. It works fine when I use Bitmap.CompressFormat.WEBP as compress format but give out NoSuchFieldError error when this method is used (Bitmap.CompressFormat.WEBP_LOSSY) as compress format. following is the code.

fun decodeImageAsPng(bitmap: Bitmap): String {
    val baos = ByteArrayOutputStream()
    bitmap.compress(Bitmap.CompressFormat.WEBP_LOSSY, 1, baos)
    val imageBytes = baos.toByteArray()
    val result = Base64.encodeToString(imageBytes, Base64.DEFAULT)
    return result
}

Below is the stack trace when this method is called

java.lang.NoSuchFieldError: No static field WEBP_LOSSY of type Landroid/graphics/Bitmap$CompressFormat; in class Landroid/graphics/Bitmap$CompressFormat; or its superclasses (declaration of 'android.graphics.Bitmap$CompressFormat' appears in /system/framework/framework.jar)
        at com.example.aphachatapp.utils.BitmapUtils.decodeImageAsPng(BitmapUtils.kt:408)
        at com.example.aphachatapp.managers.FirebaseManager$updateMyPhoto$2.apply(FirebaseManager.kt:115)
        at com.example.aphachatapp.managers.FirebaseManager$updateMyPhoto$2.apply(FirebaseManager.kt:30)
        at io.reactivex.internal.operators.maybe.MaybeFlatten$FlatMapMaybeObserver.onSuccess(MaybeFlatten.java:88)
        at io.reactivex.internal.operators.single.SingleFlatMapMaybe$FlatMapMaybeObserver.onSuccess(SingleFlatMapMaybe.java:117)
        at io.reactivex.internal.operators.maybe.MaybeCreate$Emitter.onSuccess(MaybeCreate.java:73)
        at durdinapps.rxfirebase2.RxHandler.onSuccess(RxHandler.java:36)
        at com.google.android.gms.tasks.zzn.run(Unknown Source:4)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Ashir Mehmood
  • 1,241
  • 1
  • 8
  • 10
  • You're running this on Android 11? – Mike M. Sep 22 '20 at 09:58
  • no I am testing it on android 8.1. Isn't there backward compatibility ? Cause Android studio is not giving me any compile time error or warnings for new API code. – Ashir Mehmood Sep 22 '20 at 10:07
  • 1
    No, no backward compatibility for framework classes. API level 30 (which corresponds to Android 11) means that that's the minimum version at which that field is available; the minimum version that the device must be running for you to be able to use it. – Mike M. Sep 22 '20 at 10:10
  • At lease they should give some compile time warning. Should they? – Ashir Mehmood Sep 22 '20 at 10:31
  • Yeah, it should. I'm not in front of my dev machine at the moment, though, so I can't check to see if it's the same here. – Mike M. Sep 22 '20 at 10:33
  • seem like they somehow missed to put that in their target API code detection algorithm. – Ashir Mehmood Sep 22 '20 at 18:23

0 Answers0