Example usage for android.util Base64 encode

List of usage examples for android.util Base64 encode

Introduction

In this page you can find the example usage for android.util Base64 encode.

Prototype

public static byte[] encode(byte[] input, int flags) 

Source Link

Document

Base64-encode the given data and return a newly allocated byte[] with the result.

Usage

From source file:Main.java

public static String encode(String binaryData) {
    if (TextUtils.isEmpty(binaryData)) {
        return "";
    }/*from  w  w w.j  a va2  s .c o  m*/
    return new String(Base64.encode(binaryData.getBytes(), Base64.DEFAULT));

}

From source file:Main.java

public static String fromBytes(byte[] bytes) {
    try {/* ww w.j  ava 2s  .  co m*/
        return new String(Base64.encode(bytes, Base64.NO_WRAP), CHARSET);
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static String encodeCredentials(String username, String password) {
    String cred = username + ":" + password;
    String encodedValue = null;//from w  w w.j  a v a2  s.c om
    byte[] encodedBytes = Base64.encode(cred.getBytes(), Base64.NO_WRAP);
    encodedValue = new String(encodedBytes);
    System.out.println("encodedBytes " + new String(encodedBytes));
    byte[] decodedBytes = Base64.decode(encodedBytes, Base64.NO_WRAP);
    System.out.println("decodedBytes " + new String(decodedBytes));
    return encodedValue;
}

From source file:Main.java

public static byte[] base64EncodeByte(final byte[] data) {
    return Base64.encode(data, Base64.DEFAULT);
}

From source file:Main.java

public static String getBase64(String str) {
    String result = "";
    if (str != null) {
        try {//from  w  ww .  jav  a 2 s  .  co m
            result = new String(Base64.encode(str.getBytes("utf-8"), Base64.NO_WRAP), "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    return result;
}

From source file:Main.java

public static String enThunder(String url) {
    url = "AA" + url + "ZZ";
    try {/*from   w w w  .ja va  2 s.  co m*/
        byte[] bytes = url.getBytes("GBK");
        String temp = "" + new String(Base64.encode(bytes, Base64.DEFAULT));
        url = "";
        char[] base64EncodeChars = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
                'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
                'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
                'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '=' };
        for (int i = 0; i < temp.length(); i++)
            for (int j = 0; j < base64EncodeChars.length; j++) {
                if ((int) temp.charAt(i) == (int) base64EncodeChars[j]) {
                    url = url + temp.charAt(i);
                    break;
                }
            }
        url = "thunder://" + url;
        // System.out.println(key);
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return url;
}

From source file:Main.java

public static String encoded(String data) throws UnsupportedEncodingException {
    //           byte[] b = Base64.encodeBase64(data.getBytes(ENCODING));
    byte[] b = Base64.encode(data.getBytes(ENCODING), Base64.DEFAULT);
    return new String(b, ENCODING);
}

From source file:Main.java

public static String encodedSafe(String data) throws UnsupportedEncodingException {
    //           byte[] b = Base64.encodeBase64(data.getBytes(ENCODING),true);
    byte[] b = Base64.encode(data.getBytes(ENCODING), Base64.URL_SAFE);
    return new String(b, ENCODING);
}

From source file:Main.java

public static String encodeBitmap(Bitmap bitmap, Bitmap.CompressFormat compressFormat) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(compressFormat, 100, baos);
    byte[] bytes = baos.toByteArray();
    byte[] encodedImage = Base64.encode(bytes, Base64.DEFAULT);
    return new String(encodedImage);
}

From source file:Main.java

public static String getStringMD5(String key) {
    MessageDigest md5 = null;/*  w w w  .jav a 2 s  .  co m*/
    try {
        md5 = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    md5.update(key.getBytes());
    //important: use Base64.URL_SAFE flag to avoid "+" and "/"
    return new String(Base64.encode(md5.digest(), Base64.URL_SAFE));
}