Android base 64编码字符串不显示实际图像

时间:2015-11-18 05:38:26

标签: java android image

您好我正在通过base 64编码和解码处理android图像处理。我需要将编码的基本64字符串作为字符串发送到服务器。当我在android端尝试它时,我能够将其解码为原始图像。我的解码和编码如下

public void base(Bitmap bitmap)
    {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
        byte[] byteArray = byteArrayOutputStream .toByteArray();
        String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
        toimag(encoded);
        String a=encoded.replaceAll("\\s+","");
        Log.i("strrr",a);
    }

    public void toimag(String encodedImage)
    {
        byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        logo.setImageBitmap(decodedByte);
    }

但我无法使用http://www.askapache.com/online-tools/base64-image-converter/获取实际图像。它只显示了我实际图像的一部分。这是我的基础64字符串

iVBORw0KGgoAAAANSUhEUgAAAyAAAAJYCAIAAAAVFBUnAAAAA3NCSVQICAjb4U/gAAAgAElEQVR4
    nKS9WbMsyXEm9n0emVVnu/vSDTTQQAMgOCMQHIDkiGM2Gs1QkkmU5kG/QX9QZjKZzYNMD3oZDheJ
    Q2rEAUgNFwCNbqBB9N3vOacqw10P7h4RWXXORdOUuKjOk5WVGYsvny/hwf/p6SMAAMxMLE4AGAuA
    QgJKAwFAxTCpkEaDAFCjKdTMbDZDHCoASUABoAhJkvktRKT96e/yP0UEANXMf4HCIiIiMpGUKkZC
    CKE/QUCSC5Wkt5wksxWlFDOLvpj5K0jSCEDy1d54AALzl0FNVc2MBpJLUQAwMQPV1KrqoqpQq6aq
    WmGqWlUXU1UtYIVVUzUutEW1mpmZclbYAqvgIlJV92bV9HozmZkqDFBKBRbYHqpSjKjEAlaKCoxi
    xN7UIEpAigorBSxKmOxaH8dPo/qA0CA+zgaS1eJcaAAKSAENPioCnwAfZB9c9WcWkKYk/WlUnxaK
    SIFJG+e6+ADGpOe8KKRTQp7QsIiMJNFOlPB59IlK4uzPMbYn+zM1289GeCRNat7PeDIMgGgd6FP8
    vWZWSgnKSabwLogIB/KmAQwKEaAoaPBBVDMzW9Bf1w4lNPgGCiMKygRSROayAcAipBgLhDAhWcsS
    Y0XCJKdYvJ39qzwUNXpk4tMH+AxKDhWGE5TGvgCTcQAQoBoAQkmjGqyq6gRtBw3OMu1XCquImbJo
    W1HGbLFMRHEuNqERwolFSGpysN/fJ7Hzr3QKl9LO1Uyz98bVULTBaXIAQKGMFNLobWH1uVZdYIaq
    VtVQF13MQjK0+81sL07b0W6xIMvd4tPr3F/NzH9Rhe234yGV42NbeyYL+kFjZAJAqTXFYMyj07zI
    hJj47CkIYMEuBnBgCgBag7UluDX4ScucdFtj9ISAWY0/FfCXkzSIGZUw8fkxf4sYKCYIiU2iiJRS
    RIRq0rhUjGTqhernJUR/knqRNo/jUWCNTmyYc5o0RaOq/kMRUV0ORsC7WXVFDH2OBGIgUGAAKswI
    pdZknT7OwzPzEY1PsdDazU1hATA5JNR4bO0yfHzmVd3lWKWwUppZl7DroySBtdHzYQn27Fo7Xldt
    xxDy2RdvodUb73euOx5PgZrQZY4lyQMQznEPNW82AGKbfK5mf5XkAhNXXq6uCQgVMEcjYBtSv67L
    4o/1+XFRoERRl+dVzJuiodZyYJzrLdmtEP5eMwvZqCYGKaGVYogMxSAGZdNm2qeCGtw4Dpy/1fto
    ViVO2890IORh2jSfQAhSrPqjgJGIxMY//TWBjXyYnOaEQonDedCEiBtQgqUClkGSZJlDJoSBoJmx
    vZB0vaMItGECMb8nuzSIadxM/KDBGulbShMd9FW7sxGLqsKM6KoC4G0vaI0xE7ASNChCSLcf+Xnj
    HtcfB5+VgQxiKDoPOCVRYd4d75JPXBsyIzCopdapVQe5YrA2LxxoSvIn2gVTH0wAImKJtscTETMT
    M3MxGV0bcBItpqwDt2H02nNM6ZwGg/faR0CJnOpAVD5JpuoPdKwEBGBhaj+67ACoB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gfHSf3g4beNTMx7fpliC+u/b+mFQ48BAx80YGiiZjNWfD8S
    gU+ptQavW+iHNo9j9sdSp+fJaEHxoAMHoz2iK79SEYqcoXahaVxmc/02hyI2oYf2VgPsLkpa8LMb
    WEIWhUMdCYayCGMk+7YsnBIdBjNQCj93yhtcu6OFYSJBvjKFyuEU9xAmFGGoHIT136zywFiDSrsB
    wCab+wB1f6tESJuWKi2EWhOa3WrDgEfRQNI7VYuImI15OGaAmgpliNB1zCG3+LQaYwzYS5Vj3slo
    p1JTdIpCIqTl9AcFBXQDVBxjDdQ8uPYxItf8QAHpfkJaSYEmHUqtp6Ah+hzxUYC6 

如何解决此问题?这个编码的字符串有什么问题吗?请帮忙。在此先感谢:)

2 个答案:

答案 0 :(得分:1)

看来你正在从logcat切割和粘贴。 logcat会截断长消息,因此您不应该依赖它来复制大量数据。您应该将base64字符串保存到文件中,然后使用该文件尝试验证。

答案 1 :(得分:1)

将图像编码为Base64

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
String imageEncode = Base64.encodeToString(b,
Base64.DEFAULT);

用于解码图片

byte[] decodedByte = Base64.decode(imageEncode, 0);
Bitmap newBitmap = BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
logo.setImageBitmap(newBitmap);