简体   繁体   English

保存的图像base64错误

[英]Saved Image base64 is wrong

i am trying to save some images to a web server. 我正在尝试将一些图像保存到Web服务器。 So what I doe is I convert my file in android to Base64 string, send it to my server and save the Base64 string in my database. 所以我要做的是将android中的文件转换为Base64字符串,将其发送到服务器,然后将Base64字符串保存在数据库中。 When I need, I read the string from that database and decode it. 在需要时,我从该数据库中读取字符串并对其进行解码。 But there is some problem with my image because the image is all grey. 但是我的图像有些问题,因为图像全是灰色的。

This is my code to convert the image to base64: 这是我的代码将图像转换为base64:

BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), options);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int imageWidth = options.outWidth;


if (imageWidth > widthPixels) {
    myBitmap.compress(Bitmap.CompressFormat.JPEG, 50, byteArrayOutputStream);
} else {
    myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
                                }

byte[] byteArray = byteArrayOutputStream.toByteArray();
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);

Then I send the string "encoded" to the server and save it in the database. 然后,我将字符串“ encoded”发送到服务器,并将其保存在数据库中。

And this is part of my image saved in the server 这是我保存在服务器中的图像的一部分

/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkz ODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2Nj / 9j / 4AAQSkZJRgABAQAAAQABAAD / 2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkz ODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxLZ2P / 2wBDARECYhjNJ2Nj

for some reason there are some "\\n" and I think that this is causing my problem. 由于某种原因,存在一些“ \\ n”,我认为这是造成我的问题的原因。 I have already tried to remove them with a replaceAll but didn't worked. 我已经尝试用replaceAll删除它们,但是没有用。

This is the output I get 这是我得到的输出

在此处输入图片说明

This is my database structure: 这是我的数据库结构:

在此处输入图片说明

I'm saving the encoded 64base image as a string in the column caled "imagemBase". 我将已编码的64base图像保存为字符串,列为“ imagemBase”。 Could the problem be in the type or in the codification that i'm using? 问题可能出在我使用的类型还是编码中?

I using this methods an worked for me: 我使用这种方法为我工作:

public static String encodeBitmap(Bitmap bitmap) {
    if (bitmap != null) {
        bitmap = resize(bitmap, 800, 800);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] byteArrayImage = baos.toByteArray();
        return Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
    }
    return null;
}

public static Bitmap decodeBitmap(String encodedImage) {
    byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
    return BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
}

public static Bitmap resize(Bitmap image, int maxWidth, int maxHeight) {
    if (maxHeight > 0 && maxWidth > 0) {
        int width = image.getWidth();
        int height = image.getHeight();
        float ratioBitmap = (float) width / (float) height;
        float ratioMax = (float) maxWidth / (float) maxHeight;

        int finalWidth = maxWidth;
        int finalHeight = maxHeight;
        if (ratioMax > ratioBitmap) {
            finalWidth = (int) ((float) maxHeight * ratioBitmap);
        } else {
            finalHeight = (int) ((float) maxWidth / ratioBitmap);
        }
        image = Bitmap.createScaledBitmap(image, finalWidth, finalHeight, true);
        return image;
    } else {
        return image;
    }
}

I have solved the problem. 我已经解决了问题。 When I converted the image to base64 the string that I received hade some + cahrs in it and for some reason when I sent it to the server the + was replaced with a blank space. 当我将图像转换为base64时,收到的字符串中包含一些+字符,由于某种原因,当我将其发送到服务器时,该+字符被替换为空格。 I needed to make a replace on the php file where I replaced all black spaces with the + character before saving the string in the database. 在将字符串保存到数据库之前,我需要在php文件中进行替换,在其中我用+字符替换了所有黑色空格。

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

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