String和Bitmap相互转换

/**
 * Base64字符串转换成图片
 *
 * @param string
 * @return
 */
public static Bitmap stringToBitmap(String string) {
    Bitmap bitmap = null;
    try {
        byte[] bitmapArray = Base64.decode(string, Base64.DEFAULT);
        bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bitmap;
}

/**
 * 图片转换成base64字符串
 *
 * @param bitmap
 * @return
 */
public static String bitmapToString(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] imgBytes = baos.toByteArray();// 转为byte数组
    return Base64.encodeToString(imgBytes, Base64.DEFAULT);
}

你可能感兴趣的:(MyUtils)