图片无法使用Base64编码

时间:2019-06-27 14:24:43

标签: android image kotlin base64

当我尝试在Android(Kotlin)中将照片编码为Base64时,我没有获得正确的字符串。

    private fun encodeImage(inputStream: InputStream?): String {
        inputStream?.let {
            inputStream.use {
                ByteArrayOutputStream().use { outputStream ->
                    Base64OutputStream(outputStream, Base64.NO_WRAP).use { base64FilterStream ->
                        it.copyTo(base64FilterStream)
                        base64FilterStream.flush()
                        return outputStream.toString()
                    }
                }
            }
        }
        return ""
    }

使用在线编码器时,您会得到:

  

/ 9J / 4AAQSkZJRgABAQEASABIAAD / 4QAiRXhpZgAATU0AKgAAAAgAAQESAAMAAAABAAEAAAAAAAD / 4gxYSUNDX1BST0ZJTEUAAQEAAAxITGlubwIQAABtbnRyUkdCIFhZWiAHzgACAAkABgAxAABhY3NwTVNGVAAAAABJRUMgc1JHQgAAAAAAAAAAAAAAAAAA9tYAAQAAAADTLUhQICAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABFjcHJ0AAABUAAAADNkZXNjAAABhAAAAGx3dHB0AAAB8AAAABRia3B0AAACBAAAABRyWFlaAAACGAAAABRnWFlaAAACLAAAABRiWFlaAAACQAAAABRkbW5kAAACVAAAAHBkbWRkAAACxAAAAIh2dWVkAAADTAAAAIZ2aWV3AAAD1AAAACRsdW1pAAAD + AAAABRtZWFzAAAEDAAAACR0ZWNoAAAEMAAAAAxyVFJDAAAEPAAACAxnVFJDAAAEPAAACAxiVFJDAAAEPAAACAx0ZXh0AAAAAENvcHlyaWdodCAoYykgMTk5OCBIZXdsZXR0LVBhY2thcmQgQ29tcGFueQAAZGVzYwAAAAAAAAASc1JHQiBJRUM2MTk2Ni0yLjEAAAAAAAAAAAAAABJzUkdCIElFQzYxOTY2LTIuMQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWFlaIAAAAAAAAPNRAAEAAAABFsxYWVogAAA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jbpNyQ3YDecN9c4FDhQOIw4yDkFOUI5fzm8Ofk6Njp0OrI67zstO2s7qjvoPCc8ZTykPOM9I j1hPaE94D4gPmA + oD7gPyE / YT + IP + JAI0BkQKZA50EpQWpBrEHuQjBCckK1QvdDOkN9Q8BEA0RHRIpEzkUSRVVFmkXeRiJGZ0arRvBHNUd7R8BIBUhLSJFI10kdSWNJqUnwSjdKfUrESwxLU0uaS + JMKkxyTLpNAk1KTZNN3E4lTm5Ot08AT0lPk0 / dUCdQcVC7UQZRUFGbUeZSMVJ8UsdTE1NfU6pT9lRCVI9U21UoVXVVwlYPVlxWqVb3V0RXklfgWC9YfVjLWRpZaVm4WgdaVlqmWvVbRVuVW + VcNVyGXNZdJ114XcleGl5sXr1fD19hX7NgBWBXYKpg / GFPYaJh9WJJYpxi8GNDY5dj62RAZJRk6WU9​​ZZJl52Y9ZpJm6Gc9Z5Nn6Wg / aJZo7GlDaZpp8WpIap9q92tPa6dr / 2xXbK9tCG1gbbluEm5rbsRvHm94b9FwK3CGcOBxOnGVcfByS3KmcwFzXXO4dBR0cHTMdSh1hXXhdj52m3b4d1Z3s3gReG54zHkqeYl553pGeqV7BHtje8J8IXyBfOF9QX2hfgF + Yn7CfyN / HH / lgEeAqIEKgWuBzYIwgpKC9INXg7qEHYSAhOOFR4Wrhg6GcobXhzuHn4gEiGmIzokziZmJ / opkisqLMIuWi / yMY4zKjTGNmI3 / jmaOzo82j56QBpBukNaRP5GokhGSepLjk02TtpQglIqU9JVflcmWNJaflwqXdZfgmEyYuJkkmZCZ / JpomtWbQpuvnByciZz3nWSd0p5Anq6fHZ + LN / qgaaDYoUehtqImopajBqN2o + akVqTHpTilqaYapoum / adup + CoUqjEqTepqaocqo + rAqt1q + msXKzQrUStuK4trqGvFq + LsACwdbDqsWCx1rJLssKzOLOutCW0nLUTtYq2AbZ5tvC3aLfguFm40blKucK6O7q1uy67p7whvJu9Fb2Pvgq + hL7 / v3q / 9cBwwOzBZ8Hjy / C28NY MZGxsP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ebn6Onq8vP09fb3 + PN6 / 9oADAMBAAIRAxEAPwDvNZ / 4J5Jd3Exjma2kk4jQ2DlQnB81TlQVKByNoduBlQHBHO3n7A2oJBG8Jt7pWliSF49Tksrq1RVDNsEzbUDoBh3Gzd93ONteiaf4YurK3VYfiT4rW5Z9qhL67t2Zm4G1Wj4PPB3HrjmvNf2m77xl8JBptnJ4 + 8eeG9RkvPP / AOJ1Be3Eeq20IEkixzSGOFl3Eq2xmkAxkoME / wBkVuOcywkb1MSmuitq / RKzt317rqfxRktHLc7xjo5Vh6l1q5KS5FvvK7SvstLvS2x3Ft + wf4C8OaDrmrSad4okuFaNoRZ + L9O1i8s2ZnxMsUAj + 0 + WwDHzFZNyBRn52XB + GPwi0fQPDs1v8SPDOoeKJZLw3yyy6Ddfb5pHU + ZHNLmN5FTfGfLimURkruR8kpJ4S0u88Q + DdN162v8AW7WDWtOg1GNLydmYxzxgpkSQyMDyvzAqMAEEgio4dKt7WJvtE63DR4jdpPtjOD2AK2xA4A5UjIGCTjFTHPKtajL6xiXNSad9bqy05W5Nru7PX8TgrceYXDYpUY4aUJ07pxaVr31utLtba6I4H9v79tC8 + GHjbxJ8N / CTWPneZGmo6hEBHJpERjjlZY7tHy883mFCcRvB5RAG51ZPm3wH8S7HwxqFvdat8OfBvi + KG5lne18RW1zIImZvmLqLjLyHgl5AXy2OQBRRX800cxr1qKqVZXd5K + U1Z + suG8jwOAwcI4Okocyi5WSV3bd93 / we59a / sbftUeFf2kvjxP4Lk8EaR4G1q60SOeOy0 + 2 + y6PeXMMqQRtbRyRq / mNbsiOIlwPskRYNvJT6tj + B82niSNbbcYz5Zkhmkt5F78mPG4deS2QeCM5oorza3EGOw1WVClO0Vay + R + YeInBuUyx0MZ7JKdRNyasru7V9t 7I // 9k =

但是Kotlin方法返回:

  

/ 9J / 4AAQSkZJRgABAQEASABIAAD / 4QAiRXhpZgAATU0AKgAAAAgAAQESAAMAAAABAAEAAAAAAAD / 4gxYSUNDX1BST0ZJTE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VcNVyGXNZdJ114XcleGl5sXr1fD19hX7NgBWBXYKpg / GFPYaJh9WJJYpxi8GNDY5dj62RAZJRk6WU9​​ZZJl52Y9ZpJm6Gc9Z5Nn6Wg / aJZo7GlDaZpp8WpIap9q92tPa6dr / 2xXbK9tCG1gbbluEm5rbsRvHm94b9FwK3CGcOBxOnGVcfByS3KmcwFzXXO4dBR0cHTMdSh1hXXhdj52m3b4d1Z3s3gReG54zHkqeYl553pGeqV7BHtje8J8IXyBfOF9QX2hfgF + Yn7CfyN / HH / lgEeAqIEKgWuBzYIwgpKC9INXg7qEHYSAhOOFR4Wrhg6GcobXhzuHn4gEiGmIzokziZmJ / opkisqLMIuWi / yMY4zKjTGNmI3 / jmaOzo82j56QBpBukNaRP5GokhGSepLjk02TtpQglIqU9JVflcmWNJaflwqXdZfgmEyYuJkkmZCZ / JpomtWbQpuvnByciZz3nWSd0p5Anq6fHZ + LN / qgaaDYoUehtqImopajBqN2o + akVqTHpTilqaYapoum / adup + CoUqjEqTepqaocqo + rAqt1q + msXKzQrUStuK4trqGvFq + LsACwdbDqsWCx1rJLssKzOLOutCW0nLUTtYq2AbZ5tvC3aLfguFm40blKucK6O7q1uy67p7whvJu9Fb2Pvgq + hL7 / v3q / 9cBwwOzBZ8Hjy / C28NY MZGxsPHQce / yD3IvMk6ybnKOMq3yzbLtsw1zLXNNc21zjbOts83z7jQOdC60TzRvtI / 0sHTRNPG1EnUy9VO1dHWVdbY11zX4Nhk2OjZbNnx2nba + 9uA3AXcit0Q3ZbeHN6i3ynfr + A24L3hROHM4lPi2 + NJ4 + VKC + T85YTmDeaW5x / nqegy6LzpRunQ6lvq5etw6 / vshu0R7ZzuKO6070DvzPBY8OXxcvH / 8ozzGfOn9DT0wvVQ9d72bfb794r4Gfio + Tj5x / PX + uf7d / WH / Jj9Kf26 / KV + 3P9t //// 2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwk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HzFZNyBRn52XB + GPwi0fQPDs1v8SPDOoeKJZLw3yyy6Ddfb5pHU + ZHNLmN5FTfGfLimURkruR8kpJ4S0u88Q + DdN162v8AW7WDWtOg1GNLydmYxzxgpkSQyMDyvzAqMAEEgio4dKt7WJvtE63DR4jdpPtjOD2AK2xA4A5UjIGCTjFTHPKtajL6xiXNSad9bqy05W5Nru7PX8TgrceYXDYpUY4aUJ07pxaVr31utLtba6I4H9v79tC8 + GHjbxJ8N / CTWPneZGmo6hEBHJpERjjlZY7tHy883mFCcRvB5RAG51ZPm3wH8S7HwxqFvdat8OfBvi + KG5lne18RW1zIImZvmLqLjLyHgl5AXy2OQBRRX800cxr1qKqVZXd5K + U1Z + suG8jwOAwcI4Okocyi5WSV3bd93 / we59a / sbftUeFf2kvjxP4Lk8EaR4G1q60SOeOy0 + 2 + y6PeXMMqQRtbRyRq / mNbsiOIlwPskRYNvJT6tj + B82niSNbbcYz5Zkhmkt5F78mPG4deS2QeCM5oorza3EGOw1WVClO0Vay + R + YeInBuUyx0MZ7JKdRNyasru7V9t 7I /

1 个答案:

答案 0 :(得分:0)

问题是您要在outputStream关闭(并刷新)之前返回结果,因此您丢失了仍在缓冲的任何字节数。 作为解决方案,您只需将return outputStream.toString()下移一行。

这是一个例子(我使用另一个Base64库,因为我没有使用Android运行时,但是结果是相同的):

使用您的代码,字符串some data whose length is more than thirty-two bytes被编码为c29tZSBkYXRhIHdob3NlIGxlbmd0aCBpcyBtb3JlIHRoYW4gdGhpcnR5LXR3byBieXRl,这是错误的。通过修复代码,我得到了c29tZSBkYXRhIHdob3NlIGxlbmd0aCBpcyBtb3JlIHRoYW4gdGhpcnR5LXR3byBieXRlcw==

固定代码为:

fun main() {
    val string = "some data whose length is more than thirty-two bytes"

    val inputStream = string.byteInputStream()
    inputStream.let {
        inputStream.use {
            ByteArrayOutputStream().use { outputStream ->
                Base64OutputStream(outputStream).use { base64FilterStream ->
                    it.copyTo(base64FilterStream)
                    base64FilterStream.flush()
                }
                println(outputStream.toString())
            }
        }
    }
}