谁能告诉我如何将任何格式的图像直接转换为字符串?

时间:2018-09-01 05:03:39

标签: android string base64

private String getBase64String() {

    // give your image file url in mCurrentPhotoPath
    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    // In case you want to compress your image, here it's at 40%

// here i use JPEG image but now can anyone tell how can i convert any format image in String 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 40, byteArrayOutputStream);
    byte[] byteArray = byteArrayOutputStream.toByteArray();

    return Base64.encodeToString(byteArray, Base64.DEFAULT);
}

1 个答案:

答案 0 :(得分:0)

  

您可以使用Base64 Android类:

@Value("#{mysql.databaseName}")
private String databaseName;
  

不过,您必须将图像转换为字节数组。

String ecodImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);