androidkotlinbitmapbase64android-bitmap

Issue converting Base 64 to Bitmap in Kotlin - Android


I am using below method to convert Base64 Image to Bitmap:

fun convertString64ToImage(base64String: String): Bitmap {
   val decodedString = Base64.getDecoder().decode(base64String)
   return BitmapFactory.decodeByteArray(decodedString, 0, decodedString.size)
}

But Unfortunately getting error as below:

Illegal base64 character a

FATAL EXCEPTION: main Process: com.myapp.app, PID: 27391 java.lang.IllegalArgumentException: Illegal base64 character a at java.util.Base64$Decoder.decode0(Base64.java:714) at java.util.Base64$Decoder.decode(Base64.java:526) at java.util.Base64$Decoder.decode(Base64.java:549) at com.myapp.sdk.view.BaseActivity$Utility.convertString64ToImage(BaseActivity.kt:201) at com.myapp.sdk.view.document.ScanActivity.setResultToView(ScanActivity.kt:426) at com.myapp.sdk.view.document.ScanActivity.completion$lambda-3(DocumentScanActivity.kt:274) at com.myapp.sdk.view.document.ScanActivity.$r8$lambda$mFUznctDAtk-c_-MzvUsX70AtrY(Unknown Source:0)

What might be the issue?


Solution

  • You use the wrong class.
    You need to use android.util.Base64 instead of java.util.Base64. Then call Base64.decode() method.