gpt4 book ai didi

java - 如何将图像的字节数组转换为表示 jpg 的 base64 编码字符串

转载 作者:太空宇宙 更新时间:2023-11-03 11:07:59 26 4
gpt4 key购买 nike

我正在编写一个拍照并上传照片的 Android 应用。

API 需要一个表示要上传的 jpg 的 base 64 编码字符串。

Camera API 给了我一个 byte []

我怎样才能将这个字节数组转换成 base64 编码的字符串,确保我保持 jpg 格式?

目前,我正在尝试 String encoded = Base64.encodeToString(byteArray, Base64.NO_WRAP);

但这在某种程度上是无效的,在日志中给我一堆 AAAAAAAAA 而不是真正的 base64 编码字符串。

感谢您的帮助

编辑:

其实当我注销上面encoded的内容时,我得到了

 /9j/4UVIRXhpZgAATU0AKgAAAAgACQEPAAIAAAAPAAAAegEQAAIAAAAJAAAAigESAAMAAAABAAEAAAEaAAUAAAABAAAAlAEbAAUAAAABAAAAnAEoAAMAAAABAAIAAAITAAMAAAABAAEAAIdpAAQAAAABAAAApIglAAQAAAABAAAGCAAABi5MRyBFbGVjdHJvbmljcwAAVlM5ODUgNEcAAAAAAEgAAAABAAAASAAAAAEAFIKaAAUAAAABAAABmoKdAAUAAAABAAABoognAAMAAAABAGQAAJAAAAcAAAAEMDIyMJADAAIAAAAUAAABqpAEAAIAAAAUAAABvpEBAAcAAAAEAQIDAJIEAAoAAAABAAAB0pIHAAMAAAABAAIAAJIJAAMAAAABAAAAAJIKAAUAAAABAAAB2pJ8AAcAAAIAAAAB4pKGAAcAAAIAAAAD4qAAAAcAAAAEMDEwMKABAAMAAAABAAEAAKACAAQAAAABAAAFAKADAAQAAAABAAADwKAFAAQAAAABAAAF6qQDAAMAAAABAAAAAKQEAAUAAAABAAAF4gAAAAAAAAABAAAAGAAAAMgAAABkMjAxNTowNjowNSAyMjoxMzoxOAAyMDE1OjA2OjA1IDIyOjEzOjE4AAAAAAAAAAABAAAHCAAAA+hBTENTSUlGMFUAAAAAAAAAAQAPJ3ECAA0UIBAHEhUAAElJRjEAAAAAAABsAOsAAACdANAATACD/w0AcAAbAHUBmf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XU1zp0/+4a51h8ufSuav0OvD7MUL+5XjuRSxkB04pV5hHsaBgMp5rGHxI3lsbqqrIMgHimvbRMOUFTIMxr9Kdtr0rI47sxr23jiKlOOelERZo8lcH61b1GP5EPvV

但是当它实际发布到服务器时(使用 Square 的 Retrofit 库),我在这之后收到了数十万个 AAAAA,导致服务器崩溃。

尽管如此,记录的编码字符串似乎不够长,无法封装与 LG G3 照片相关联的所有数据。

最佳答案

使用

byte [] data = Base64.decode(base64str);

您需要引用 commons codec从您的项目中提取,以便该代码正常工作。

关于java - 如何将图像的字节数组转换为表示 jpg 的 base64 编码字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30678233/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com