Example usage for android.util Base64 DEFAULT

List of usage examples for android.util Base64 DEFAULT

Introduction

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

Prototype

int DEFAULT

To view the source code for android.util Base64 DEFAULT.

Click Source Link

Document

Default values for encoder/decoder flags.

Usage

From source file:Main.java

public static String base64Encode(ImageView imageView) {
    if (imageView.getDrawable() != null) {
        Bitmap avatarBitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        avatarBitmap.compress(Bitmap.CompressFormat.PNG, 70, baos);
        byte[] avatarByteArray = baos.toByteArray();
        return Base64.encodeToString(avatarByteArray, Base64.DEFAULT);
    } else {/*  w  ww  . j ava 2  s  .c o m*/
        return null;
    }
}

From source file:Main.java

public static String BitmapToBase64(Bitmap bitmap) {

    String result = null;//  w w w  .  j  ava  2 s .  c o m
    ByteArrayOutputStream baos = null;
    try {
        if (bitmap != null) {
            baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 60, baos);

            baos.flush();
            baos.close();

            byte[] bitmapBytes = baos.toByteArray();
            result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (baos != null) {
                baos.flush();
                baos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;
}

From source file:Main.java

public static String convertIconToString(Bitmap bitmap) {
    String result = null;//from  w ww . ja v a  2s. com
    ByteArrayOutputStream baos = null;
    try {
        if (bitmap != null) {
            baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

            baos.flush();
            baos.close();

            byte[] bitmapBytes = baos.toByteArray();
            result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (baos != null) {
                baos.flush();
                baos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;

}

From source file:Main.java

public static String imgsToBase64(String filePath) {
    Bitmap bitmap = BitmapFactory.decodeFile(filePath);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    String lastName = filePath.substring(filePath.lastIndexOf(".") + 1);
    if (lastName.equals(Bitmap.CompressFormat.JPEG)) {
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
    } else {/*w  w w .j ava2  s. c  o  m*/
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
    }
    return Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT);
}

From source file:Main.java

public static byte[] decodeBase64(byte[] input) {
    return Base64.decode(input, Base64.DEFAULT);
}

From source file:Main.java

public static byte[] encodeBase64(byte[] input) {
    return Base64.encode(input, Base64.DEFAULT);
}

From source file:Main.java

public static String base64Decode(String str) {
    return Base64.decode(str.getBytes(), Base64.DEFAULT).toString();
}

From source file:Main.java

public static void printHashKey(Context context) {
    try {/*from   w  w  w  .  j  a v a  2s .c  o m*/
        PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(),
                PackageManager.GET_SIGNATURES);
        for (android.content.pm.Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.e("HASH KEY:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    } catch (PackageManager.NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }
}

From source file:Main.java

public static String base64Encode(String str) {
    return Base64.encodeToString(str.getBytes(), Base64.DEFAULT);
}

From source file:Main.java

/**
 * Compresses a bitmap into a PNG and converts into a Base64 encoded string.
 * The encoded string can be decoded using {@link decodeBitmapFromString(String)}.
 * @param bitmap The Bitmap to compress and encode.
 * @return the String encoding the Bitmap.
 *//*from  w w  w  .j  a  va  2 s.c  o  m*/
public static String encodeBitmapAsString(Bitmap bitmap) {
    if (bitmap == null)
        return "";
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
    return Base64.encodeToString(output.toByteArray(), Base64.DEFAULT);
}