简体   繁体   English

如何在数据库中将图像LONGBLOB从数据库转换为base64编码的字符串?

[英]How to convert an image LONGBLOB from the database to base64 encoded string in android?

i have a String longblobimage, that is taken from the mysql database how do i convert it to a base64 String, and how do i know if its already an encoded image or not? 我有一个字符串longblobimage,它取自mysql数据库,我如何将其转换为base64字符串,又如何知道其是否已编码图像?

here is my attempt: 这是我的尝试:

 public String checkForEncode(String string) {
    String regex = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
    if(string.matches(regex)){
        System.out.println("ERROR33:it's a Base64");
        return string;
    }
    String encodedImage = Base64.encodeToString(string.getBytes(), Base64.DEFAULT);
    return encodedImage;
}

so far this doesnt work to me for some reason 到目前为止,由于某种原因,这对我不起作用

What are you importing to get access to 您要导入什么才能访问

Base64.encodeToString(string.getBytes(), Base64.DEFAULT) Base64.encodeToString(string.getBytes(),Base64.DEFAULT)

I'm trying to figure out how this is working, but the Base64 doesn't even have a documented static method that takes an encoding type. 我试图弄清楚它是如何工作的,但是Base64甚至没有记录下来的采用编码类型的静态方法。

You can translate a string to a base64 byteArray, which is how you would probably store the image to a file. 您可以将字符串转换为base64 byteArray,这可能是将图像存储到文件中的方式。

A String in Java is UTF-16 encoded, so storing a Base64 encoded byteArray back into a String doesn't make sense as that changes the encoding again. Java中的String是UTF-16编码的,因此将Base64编码的byteArray重新存储到String中没有意义,因为这会再次更改编码。

byte[] encoded = Base64.encodeBase64(string.getBytes()); byte []编码= Base64.encodeBase64(string.getBytes());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM