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 setImBin(String fName) {
    FileInputStream fileInputStream = null;

    File file = new File(Environment.getExternalStorageDirectory().getPath(), "Pictures/" + fName);

    byte[] bFile = new byte[(int) file.length()];

    try {//from  w ww . j a v  a 2  s .c om
        fileInputStream = new FileInputStream(file);
        fileInputStream.read(bFile);
        fileInputStream.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return Base64.encodeToString(bFile, Base64.DEFAULT);
}

From source file:Main.java

public static String encodeToString(String content) {
    try {//from   w  ww  .  ja va  2s.c  o m
        return Base64.encodeToString(content.getBytes(), Base64.NO_WRAP);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

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 {//from w  w  w  .j  a  v  a 2s .c  o  m
        return null;
    }
}

From source file:Main.java

public static String encodeStringToStringBySystem(String dataStr, int Base64Flags) {
    byte[] data = dataStr.getBytes();
    return Base64.encodeToString(data, Base64Flags);
}

From source file:Main.java

public static String BitmapToBase64(Bitmap bitmap) {

    String result = null;/*from   w w w . j  av a  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  ww w  .ja  va  2 s  .  co m
    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 {/*from  w w w .  j  av a2s.c o  m*/
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
    }
    return Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT);
}

From source file:Main.java

public static String encodeUrlSafe64(byte[] array) {
    int flags = Base64.NO_PADDING | Base64.NO_WRAP | Base64.URL_SAFE;
    return Base64.encodeToString(array, flags);
}

From source file:Main.java

public static String makeSHA1HashBase64(byte[] bytes) {
    try {/*from  w ww  .j av  a  2  s.  c om*/
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.update(bytes, 0, bytes.length);
        byte[] sha1hash = md.digest();
        return Base64.encodeToString(sha1hash, Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

static String encodeB64NP(byte[] bytes) {
    return Base64.encodeToString(bytes, Base64.NO_PADDING);
}