Sometimes need to encode / decode some data. As example sometimes need to encode some data, …

Really simple way do it use Base64 Encoder, which has adding to Android SDK since API 8. This class contain methods for encoding and decoding the Base64 representation of binary data.

For work with Base64 Encoder in android need to add import
[java]
import android.util.Base64;
[/java]

Code snippet:
[java]
String testValue = “Hello, world!”;

byte[] encodeValue = Base64.encode(testValue.getBytes(), Base64.DEFAULT);
byte[] decodeValue = Base64.decode(encodeValue, Base64.DEFAULT);

Log.d(“TEST”, “defaultValue = ” + testValue);
Log.d(“TEST”, “encodeValue = ” + new String(encodeValue));
Log.d(“TEST”, “decodeValue = ” + new String(decodeValue));
[/java]

Output:
[java]
defaultValue = Hello, world!
encodeValue = SGVsbG8sIHdvcmxkIQ==
decodeValue = Hello, world!
[/java]

References:
Base64 | Android Developers

13 CommentsClose Comments

13 Comments

  • Craig
    Posted April 7, 2015 at 8:57 pm 0Likes

    Encoding is not the same thing as encryption. If you’re storing sensitive information, you should be encrypting, not simply Base64 encoding.

    • Posted April 7, 2015 at 9:05 pm 0Likes

      Yes, certainly, use encrypting better for sensitive information. But Base64 really good and powerful tool, which can help some developers.

  • Harsh Upadhyay
    Posted July 5, 2016 at 11:17 am 0Likes

    How can we apply the base64 encoding/decoding to the associative array?

    • Posted September 18, 2016 at 8:46 pm 0Likes

      You can to add encoded item to associative array and finally decode item from this array

  • Sakshi Tripathi
    Posted January 13, 2017 at 5:38 pm 0Likes

    How to encode and decode audio recorded from phone microphone.

    • Posted January 21, 2017 at 9:13 am 0Likes

      You can transform your record to byte array (byte[]) and encode and decode these arrays

  • Shahen
    Posted February 25, 2017 at 7:56 pm 0Likes

    how many type encoding are supported by android ?

  • mepa
    Posted May 2, 2017 at 2:33 pm 0Likes

    can we encode using blue j

    • Posted June 14, 2017 at 5:42 pm 0Likes

      Unfortunately, I never used BlueJ

  • Igor Ganapolsky
    Posted May 27, 2018 at 5:23 pm 0Likes

    Can we use this approach to obfuscate sensitive data in the app?

  • mruto
    Posted August 9, 2018 at 9:01 am 0Likes

    thank you so much for this, solves my bug perfectly

Leave a comment

Newsletter Subscribe

Get the Latest Posts & Articles in Your Email

We Promise Not to Send Spam:)