Example usage for android.util Base64 encodeToString

List of usage examples for android.util Base64 encodeToString

Introduction

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

Prototype

public static String encodeToString(byte[] input, int flags) 

Source Link

Document

Base64-encode the given data and return a newly allocated String with the result.

Usage

From source file:Main.java

public static String convertBitmapToString(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
    byte[] b = baos.toByteArray();
    String temp = null;/*w  ww.j av a2s . com*/
    try {
        System.gc();
        temp = Base64.encodeToString(b, Base64.DEFAULT);
    } catch (Exception e) {
        e.printStackTrace();
    } catch (OutOfMemoryError e) {
        baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
        b = baos.toByteArray();
        temp = Base64.encodeToString(b, Base64.DEFAULT);
    }
    return temp;
}

From source file:Main.java

public static String openAsBase64(String imgPath) {
    Bitmap photo = BitmapFactory.decodeFile(imgPath);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    photo.compress(Bitmap.CompressFormat.JPEG, 85, baos);
    byte[] b = baos.toByteArray();
    photo.recycle();//from  w  w  w .j a v a  2  s .c o m
    return Base64.encodeToString(b, Base64.DEFAULT);

}

From source file:Main.java

/**
 * This method converts bitmap to base64 string.
 * // ww  w.j  a  v  a2  s  .c  o  m
 * @param bitmap
 * @return base64EncodedImage
 */
public static String encodeTobase64(Bitmap bitmap) {
    // TODO Auto-generated method stub
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] b = baos.toByteArray();
    return Base64.encodeToString(b, Base64.DEFAULT);
}

From source file:Main.java

public static String serialize(Bitmap poster) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    poster.compress(FORMAT, QUALITY, byteArrayOutputStream);
    return Base64.encodeToString(byteArrayOutputStream.toByteArray(), BASE64_FLAGS);
}

From source file:Main.java

public static String encrypt(String data, String key) throws Exception {
    try {/*from www.jav  a 2  s .co  m*/
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        byte[] base64Key = Base64.decode(key, Base64.DEFAULT);
        SecretKeySpec keyspec = new SecretKeySpec(base64Key, "AES");
        cipher.init(Cipher.ENCRYPT_MODE, keyspec);
        byte[] encrypted = cipher.doFinal(data.getBytes("UTF-8"));
        return Base64.encodeToString(encrypted, Base64.DEFAULT);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String convertToBase64(Bitmap bmp) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 80, stream);
    byte[] byteArray = stream.toByteArray();
    try {//from  w ww .ja  v a2s .  c  o m
        stream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return Base64.encodeToString(byteArray, Base64.DEFAULT);
}

From source file:Main.java

public static String encodeToStringBySystem(byte[] data, int Base64Flags) {

    return Base64.encodeToString(data, Base64Flags);
}

From source file:Main.java

public static String generateShortUuid() {
    UUID uuid = UUID.randomUUID();
    MessageDigest md = null;/*w  w  w.  jav  a2s . c om*/
    try {
        md = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return uuid.toString();
    }

    md.update(uuid.toString().getBytes());
    byte[] digest = md.digest();

    return Base64.encodeToString(digest, Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING).substring(0, 20);
}

From source file:Main.java

public static String createHMACWithMD5(String source, String key)
        throws NoSuchAlgorithmException, InvalidKeyException {
    Mac hmac = Mac.getInstance("HmacMD5");
    SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), hmac.getAlgorithm());
    hmac.init(secretKey);//from ww w  .j a  va  2  s  . c om

    byte[] signature = hmac.doFinal(source.getBytes());
    return Base64.encodeToString(signature, Base64.DEFAULT);
}

From source file:Main.java

public static String bitmapToString(String filePath) {

    Bitmap bm = getSmallBitmap(filePath);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(CompressFormat.JPEG, 40, baos);
    byte[] b = baos.toByteArray();
    return Base64.encodeToString(b, Base64.DEFAULT);
}