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

时间:2022-10-24 14:10:55

I am writing an Android app that takes photos and uploads them.

我正在编写一个Android应用程序,可以拍摄照片并上传它们。

The API requires a base 64 encoded string representing the jpg to upload.

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

The Camera API gives me a byte [].

Camera API给了我一个byte []。

How can I go about converting this byte array into a base64 encoded string, ensuring I keep jpg format?

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

Currently, I am trying String encoded = Base64.encodeToString(byteArray, Base64.NO_WRAP);

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

But this somehow is invalid, giving me a bunch of AAAAAAAAAs in the log rather than a real base64 encoded string.

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

Thanks for any help

谢谢你的帮助

EDIT:

编辑:

Actually, when I log out the contents of encoded above, I get

实际上,当我注销上面编码的内容时,我得到了

 /9j/4UVIRXhpZgAATU0AKgAAAAgACQEPAAIAAAAPAAAAegEQAAIAAAAJAAAAigESAAMAAAABAAEAAAEaAAUAAAABAAAAlAEbAAUAAAABAAAAnAEoAAMAAAABAAIAAAITAAMAAAABAAEAAIdpAAQAAAABAAAApIglAAQAAAABAAAGCAAABi5MRyBFbGVjdHJvbmljcwAAVlM5ODUgNEcAAAAAAEgAAAABAAAASAAAAAEAFIKaAAUAAAABAAABmoKdAAUAAAABAAABoognAAMAAAABAGQAAJAAAAcAAAAEMDIyMJADAAIAAAAUAAABqpAEAAIAAAAUAAABvpEBAAcAAAAEAQIDAJIEAAoAAAABAAAB0pIHAAMAAAABAAIAAJIJAAMAAAABAAAAAJIKAAUAAAABAAAB2pJ8AAcAAAIAAAAB4pKGAAcAAAIAAAAD4qAAAAcAAAAEMDEwMKABAAMAAAABAAEAAKACAAQAAAABAAAFAKADAAQAAAABAAADwKAFAAQAAAABAAAF6qQDAAMAAAABAAAAAKQEAAUAAAABAAAF4gAAAAAAAAABAAAAGAAAAMgA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JvRwf1rtStJiKsyZiNUGStaRco30rPdaQFMr7Um2pytJsqkIiC04LUgSnBaoCMLShalCU8JQBEEpwSpQtOCe1AEQWnbamCU4J7UAQhM04JUwT2p4SmBBspRH7VY2e1KEpAYDR1E0dXCPaoXHtWpzoytVQf2fJ7AH9a58qAK6XU1zp0/+4a51h8ufSuav0OvD7MUL+5XjuRSxkB04pV5hHsaBgMp5rGHxI3lsbqqrIMgHimvbRMOUFTIMxr9Kdtr0rI47sxr23jiKlOOelERZo8lcH61b1GP5EPvV

But when it actually posts to the server (using Square's Retrofit library), I get hundreds of thousands of AAAAAs after this that break the server.

但是当它实际发布到服务器(使用Square的Retrofit库)时,我会在此之后获得数十万个AAAAA,从而破坏服务器。

Despite this, though, the logged encoded string doesn't seem long enough to encapsulate all the data that would be associated with a photo from an LG G3.

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

1 个解决方案

#1


0  

Use

使用

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

you would need to reference commons codec from your project in order for that code to work.

您需要从项目中引用commons编解码器才能使代码生效。

#1


0  

Use

使用

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

you would need to reference commons codec from your project in order for that code to work.

您需要从项目中引用commons编解码器才能使代码生效。