Base64 encoder and decoder
This is an example of how to use the Base64
class to encode and decode a simple String value.
// String to be encoded with Base64String text = "Test";// Sending sidebyte[] data = null;try { data = text.getBytes("UTF-8");} catch (UnsupportedEncodingException e1) { e1.printStackTrace();}String base64 = Base64.encodeToString(data, Base64.DEFAULT);// Receiving sidebyte[] 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.
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.
Here is a simple method I was going to use until I realized that this is only supported in Android API 8+:
// Has line breakpublic String getBase64(String input) { return Base64.encodeToString(input.getBytes(), Base64.DEFAULT);}// No line breakpublic String getBase64(String input) { return Base64.encodeToString(input.getBytes(), Base64.NO_WRAP);}