Base64 encoder and decoder
Asked Answered
T

6

50

Is there a base-64 decoder and encoder for a String in Android?

Timework answered 1/12, 2010 at 7:40 Comment(0)
E
33

See android.util.Base64

It seems that this was added in API version 8 or android 2.2 so it will not be available on the older platforms.

But the source of it is at android/util/Base64.java so if needed one could just copy it unchanged for older versions.

Earnestineearnings answered 1/12, 2010 at 7:42 Comment(4)
encoded Base64 of Mw== is TXc9PQ== But this source code show TXc9PQo= What's wrong?! :-oMenashem
@Mr.Hyde You have a trailing new line. TXc9PQo= decodes to Mw==\n.Earnestineearnings
Yea, and to solve it, we should use Base64.NO_WRAP for flag in encodeToString function, instead of Base64.DEFAULT.Menashem
hi have Base64.class but its having an error "$assertionsDisabled cannot be resolved to a variable"Datha
I
77

This is an example of how to use the Base64 class to encode and decode a simple String value.

// String to be encoded with Base64
String text = "Test";
// Sending side
byte[] data = null;
try {
    data = text.getBytes("UTF-8");
} catch (UnsupportedEncodingException e1) {
    e1.printStackTrace();
}
String base64 = Base64.encodeToString(data, Base64.DEFAULT);

// Receiving side
byte[] data1 = Base64.decode(base64, Base64.DEFAULT);
String text1 = null;
try {
    text1 = new String(data1, "UTF-8");
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}

This excerpt can be included in an Android activity.

Impassable answered 22/8, 2012 at 20:50 Comment(2)
using Base64.DEFAULT will insert a line break in my case. I use Base64.NO_WRAP instead.Wallaby
The only one which works for me with 2 bytes characters.Pansophy
E
33

See android.util.Base64

It seems that this was added in API version 8 or android 2.2 so it will not be available on the older platforms.

But the source of it is at android/util/Base64.java so if needed one could just copy it unchanged for older versions.

Earnestineearnings answered 1/12, 2010 at 7:42 Comment(4)
encoded Base64 of Mw== is TXc9PQ== But this source code show TXc9PQo= What's wrong?! :-oMenashem
@Mr.Hyde You have a trailing new line. TXc9PQo= decodes to Mw==\n.Earnestineearnings
Yea, and to solve it, we should use Base64.NO_WRAP for flag in encodeToString function, instead of Base64.DEFAULT.Menashem
hi have Base64.class but its having an error "$assertionsDisabled cannot be resolved to a variable"Datha
A
13

Here is a simple method I was going to use until I realized that this is only supported in Android API 8+:

// Has line break
public String getBase64(String input) {
    return Base64.encodeToString(input.getBytes(), Base64.DEFAULT);
}

// No line break
public String getBase64(String input) {
    return Base64.encodeToString(input.getBytes(), Base64.NO_WRAP);
}
Amoebocyte answered 26/8, 2012 at 16:6 Comment(1)
as @arvin commented above: "using Base64.DEFAULT will insert a line break in my case. I use Base64.NO_WRAP instead."Gaddy
M
12

To encode:

private String encodeString(String s) {
    byte[] data = new byte[0];

    try {
        data = s.getBytes("UTF-8");

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } finally {
        String base64Encoded = Base64.encodeToString(data, Base64.DEFAULT);

        return base64Encoded;

    }
}

To decode:

private String decodeString(String encoded) {
    byte[] dataDec = Base64.decode(encoded, Base64.DEFAULT);
    String decodedString = "";
    try {

        decodedString = new String(dataDec, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();

    } finally {

        return decodedString;
    }
}

Example

    String text = "example007";

    Log.e("encoded", encodeString(text)); //Output: ZXhhbXBsZTAwNw==
    Log.e("decoded", decodeString(encodeString(text))); //Output: example007
Mylonite answered 23/12, 2015 at 22:21 Comment(0)
T
5

If you don't want a line break at the end of the String, change the flags from Base64.DEFAULT to Base64.NO_WRAP

Base64.encodeToString("yourString".getBytes("UTF-8"), Base64.NO_WRAP);
Tristan answered 7/5, 2015 at 7:24 Comment(0)
P
0

Kotlin supports Base64 for versions 1.8.20 and over. It has 3 types available which are Base64.Default, Base64.UrlSafe and Base64.Mime.

// Base64.Default
val nameBytes = "Nav".map { it.code.toByte() }.toByteArray()
val encodedValue = Base64.Default.encode(nameBytes)
// Encode value: TmF2
println("Encoded: $encodedValue") 
// Decoded value: Nav
println("Decoded: ${String(Base64.Default.decode(encodedValue))}")

// Base64.UrlSafe
val googleIOUrlBytes = "google.io".map { it.code.toByte() }.toByteArray()
// Encode value: Z29vZ2xlLmlv
val encodedURLSafe = Base64.UrlSafe.encode(googleIOUrlBytes)
println("Encoded UrlSafe: $encodedURLSafe")
// Decoded value: google.io
println("Decoded UrlSafe: ${String(Base64.UrlSafe.decode(encodedURLSafe))}")
Postiche answered 11/4, 2023 at 6:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.