在Android中将位图转换为Base64字符串

时间:2015-01-12 03:57:12

标签: android

我使用以下代码将Bitmap图像转换为Base64 String格式。

public static String encodeTobase64(Bitmap image)
    {
        Bitmap immagex=image;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
        immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] b = baos.toByteArray();
        String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);

        Log.e(TAG + "----CROP_FROM_CAMERA imageEncoded-------", ""+imageEncoded);

        return imageEncoded;
    }

但是,在将Bitmap转换为Base64 String时,它会像下面一样获取String。

01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCACgAKADASIA
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): p6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): AwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): BhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): U1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD7I/bG
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): +Gl3488J2GuXltFejwB4sbxL4cjMptFt9bsIr/8As+43JG3nsLryf9GZkScFY2kABz7Ymv6X4x8D
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): fCnW9F2tpmvz+GdXtijK6z2l9pzXduDIpZcOkibiOrE91rq/jd4Uu/FHwt+IPh7S9S1DR9d1DQtb
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): Xw3q1hKEbT/Ei293Po122Q33L+O2ZjtJ2GQZzgV8NfsJfEHVfiJ+y38ObnVJJH1/4a+NLvw34oS4
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): 2ifztEvpmQTKo/dk2l9ASu1QAAqKMDP6z7CWK4UlXc4SWUZpRwrje0oUMzo166qqLT9yVbCSjL+8
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): 4XeqZ+Ue0dHOPZ3tTrUPaJpXvOlOEJLZq3LPnV3rrZPlZ9VftL+M7X4N/s9fFDx3eMsUGleFtQ+y
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): JK2xf7Uu4pLXTEQggg/bJomX+I4OeVLVwH/BP6T4peFvgz4VuPjlNDrFz8SLLTfGNhq72y217ptj
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): qun2g0LSL2OOJI/I0zSY7K1tVAT7JAI7Pb5cWRnftKT2/wAWpdF+DWteHxq3hvxTcibUIp7eSe2G
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): Ga1s1udoZEkV5Lm8tAzRbZbZZFnDIUb7pe28M6L8NNK0PU1FimkWNlpemGOAExpbxiC32BSNscKq
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): quFOEXoMAColUpYfhqlh4R/2nHZh9drVZRV/qWGhUoYWnGTvK1SvLE1altJL2PMm4XCnF18xrzbv
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): RpU4U4JO/wC+c6k6kno0rR5I63esldJXfQ+LLWLyAgZWt3t8xsCSpzvwwIyOwxzwQfx8M8P69D4f
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): 8T6l4evboJpPiO3LRvK37q31CLzAknJ2qJOjEY5xyVAI72PWJ73wxeaZcS7tT0RQUduTcWAZjHKD
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): nLDZndx025yQpr5W8baqsep6fIA6fvsHzGDPlxLycMCAfvICM/M2Tkc/Ntp6rfr+KX9ddLttXPWi
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): lFWjt8/Pze9u/fVtNvp4/FGlWPj7xDptzqKXEmmDTZ/ItzvWSNIQ+U2thlaR1XJJ3SNHF95Du+gd
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): F12+drTUtQO29vVX+ytKXOzT7Lc+66mUkbW2jLORudyqhiqMT8nfDrR7K413xN4ultPtn2vV4LK2
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): QMZJNTvNOtltrazTljFb2Di4d0ACpJLPIw81y1fSkFhe7pbV5DPrOoxq2r36j/R9E09Qxa3t+T86
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): KFRYx/EyqckkhRV7+X+clfdeWmr1W7Ur1ZxfTSz3v1l2e929H5bp3PmH/go5Y+KfjN+z9B4G8KxS
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): O2u+L7TTItRzuggh0HQfEWp3juu8HF3qNxp1vHImTi3vVZWBCn+flf2E/i+9rP8AYxYXBeLww8qN
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): uJiLfbbSQlgQzMksyMAVyy7hy5LV/U1411WfQPDfh2Ww8P2+p2y+IHtms7hTI8emLoGuv9qjwvFx
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): JNCnmOeCZZSSWOT4zpfxGzbXDy+GvDdvGdCspo4pbCQTteQ6tGWYylwNpjyy5QFCPmJAr+ovCvMM
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): zy/g+lTwFKMoVMVXqSlKaV6ixVe/uW50+SmktUrLmbd2fkXGc6E87kq8vejSpJe6tnG8eW6ta71b
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): fddXJeO/8EhP2Zdc/Z18F/FjX/GkMMXifxn4ksNEjMcbL5Gj+GftxEcbPklJ73UZ3UqVyqpuAZST
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): +vmqyPYyf2nbsWgcxLfRYOMAkLcquPvxD5ZARlo36kqN3jPhbWPN0BL21toLeBL1ZHjtExGg1CJr
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): pZBjrvkV/nbksDyTuNdhJ4liaCWCViJVjLwgksJ4lDtKuD1ljRfM2jO5MkAsmD+GcYZniM04pzvM
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): MRNyq1sXKEk2moxoWoQgmtOW1PzfxXbd2/1fIcPHC5Ll1GCslh4t6WbcnJt7db7eb33PPbqyY6nr
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): sQYPB/baXkQxlYxcQoJBHzwA6E56YIOCcGvQPDmnorTXE6AJAA7OBxLjfhD+fJPB3DP3c1574RvE
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): 1fUPE8aK7tBqtvagHG1iLOGRSuSegkXd3+5gkBifWJ5YNMtYtJQl7uePzLp1z+7yWVEI3EDljj1J
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): wTnOfnVKC9onpzKy5Ukra6bd/Xpd3Vz1elnbS23z9L9O71d9tfBvEXhSPXppLyceYzXDTbSAR94Y
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): JxkgADp0zg54yfMtW+EUWuapZajpWoXGg+JNMkSXS9Y06Rra9t5VYGNkmjKyYXIbAbuw5BYH6juW
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): XSVVJ032cyMDlcvDLuUq6t1VWOQw6nJOCRXHWtm1/efaoWNvLHIrQbgdpdcEK5/g3YHJ4I27jyCc
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): OVdt/Xe721et1utb33ad7hUnC3LLt81ro0/+H262PlT4y/BCD402PhDSv2i7DS/F134O1y01TSfF
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): mnLFYeK7/SrWZribwxqUkUX+n6RrTKI76NwixNm5AaUknqNatvFPi651FtWAsPDujWtppmieHbAG
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): DSrOH7OsEZMKBVuriK3BQXEqlo1YpCERmWvqLTPD51WObV9SjS4mea4RN6q3kRRyyQwrFuU4BCF2
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): IOSzE54rD1/S4be3uoY41VZJ2c4UDJDGMYwBnATjIPGBk4zWscPBSlVjCKlKNNSkopTqKCklzOye
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): lm1zN3u7aKR0vG4mvQo4apVnUo0JVZUoSnKUIOrOUp8kZScYXcE1ypdm7q55bovhaKw8IwII8MxB
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): UbMfu8qh4I5yOpz0yP7xr6X8HzG5+H9tp+0btHmeD0JiLM0ORgkbQWwSOBwTksa4P7DEtpbWhACw
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): W8ZkGDghjJjk+yfyG7Kgtu+Cr6O01C60S5lCR6nC0MQYhV+1Rv8Aucnd8u9Ttx15HU4qWmm7rtvp
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): 1l5q/S9tVZPrd8jd2/u/F/fv00vfe5wuqxXk1rc200QJlDeXIBlGy0pAJx97AXn+9g84JPwT4H02
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): 0+CPjTx54P0vR7LT9G8XXmteIwkFk0Nr/bv9rJqt/PPIFxNdX8OuzSg7sJb2VpBCirGwH6LeGbq2
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): 8SaM0BlDSmFfKfOHKlSUkyxJyRtYHngkAluT+Gf/AAUPl8VfA/4/fBf4yW1xeR6e9xqvgXWPOuHT
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): SJ4dTjuZtIudQyywItvOZElnkwywMXLBYhn3+FMFVzXF4rJqc5L+08JXp0IauNTH4WlUxWDhKKkr
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): SqTpyp0pvmanUjHlak7/ACeb1lg6f16SVsPKHNLqqU5KFVxv1UXzW62+K6d/Zfg5Z6r8cf8Agoh8
01-12 09:22:48.642: E/Registration----CROP_FROM_CAMERA imageEncoded-------(13711): ZPFpvbn/AIQn9n7wl

我没理解为什么编码的字符串是分裂的。帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

在对encodeToString的调用中,您需要使用标记Base64.DEFAULT,而不是使用标记Base64.NO_WRAP作为第二个参数。默认结果与RFC 2045一致,后者生成最大长度为76的行。