After trying to decode Base64, reciving E/AndroidRuntime: FATAL EXCEPTION: main

63 Views Asked by At

I´m building a simple app for android 4.4 and up, i have a method to decode base64 string using the apache-commons-codec library.

import org.apache.commons.codec.binary.Base64;

public static String decodeBase64(String toDecode) {
    byte[] decoded = Base64.decodeBase64(toDecode);

    return new String(decoded);
}

but when i call this static method from the main (like className.decodeBase64(VAR)) I receive the following error. error log

i'm currently using jdk 8 and the last version of android studio. I know that the jdk8 implements a base64 encoder/decoder but i can´t use it, because I was limited to programming to more recent android versions at 8.0

2

There are 2 best solutions below

1
On

It may be an issue with the library version. Which version of commons-codec do you use in your build.gradle file? Try updating it to the latest, which is 1.13. Also this may be the solution for your issue: Apache Commons Codec with Android: could not find method

0
On

After two days looking for it, i find a nice solution.

Just need to replace the apache.commons with android.util.Base64, who'd say...

the method would look like the following:

import android.util.Base64;

public static String DecodeBase64(String toDecode){
    byte[] decodeValue = Base64.decode(toDecode, Base64.DEFAULT);
    return new String(decodeValue);
}

also, if someone knows how to solve the original error and please explain me why that happen, it would be helpful