Android Question IMG format base64

Uniko Sistemi srl

Active Member
Licensed User
Hi everyone! in my project I receive from a server with post call, a base64 string that contains an image that I convert and display without problems. The image is in jpg format, I should also be able to accept png formats, but I don't understand if there is a way to understand from the base64 string or with some other way if the image I receive and I'm about to convert is in jpg format or png Can anyone give me some advice? Thank you all
 

Star-Dust

Expert
Licensed User
Longtime User
JPEG file headers typically begin with bytes FF D8 ("/f8=" to B64), PNG file headers begin with a fixed 8-byte signature: 89 50 4E 47 0D 0A 1A 0A ("iVBORw==" to B64)

You have to look for these after you transform to the normal format
 
Upvote 0

Uniko Sistemi srl

Active Member
Licensed User
JPEG file headers typically begin with bytes FF D8 ("/f8=" to B64), PNG file headers begin with a fixed 8-byte signature: 89 50 4E 47 0D 0A 1A 0A ("iVBORw==" to B64)

You have to look for these after you transform to the normal format

Thank you! However, I receive the image string encoded in base64, does the same thing you suggested apply? or do I have to transform it into bytes? Thank you!
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Thank you! However, I receive the image string encoded in base64, does the same thing you suggested apply? or do I have to transform it into bytes? Thank you!
The first characters that the file should have in Hexadecimal but also in Base64.
I suggest you give it a try, but I think it should work both ways.
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
An alternative could be that the server also returned the mimetype (or at least the extension), together with the base64. In that case you wouldn't have to analyze the data on the client.
 
Upvote 0
Top