1

I want to get file type of the file on provided URL. If is it a GIF file, I need to know that.

Example URL : https://media3.giphy.com/avatars/100soft/WahNEDdlGjRZ.gif (it may not always include a file extension)

I'm working on Android

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Burak Taban
  • 429
  • 1
  • 5
  • 11
  • Even if there was a file extension, it's not required to be `gif`. You have to download the file, or at least some header of an InputStream to determine the file type – OneCricketeer Aug 28 '17 at 00:33

1 Answers1

4

Get the InputStream from the URL: InputStream from a URL

Most solutions will have you reading the whole stream, but that's not completely necessary because you only need to check the first three bytes.

Convert it to a byte[] : Convert InputStream to byte array in Java

All GIF files must start with a header block. The header takes up the first six bytes of the file. These bytes should all correspond to ASCII character codes. The first three bytes are called the signature. These should always be "GIF" (ie 47="G", 49="I", 46="F")

http://giflib.sourceforge.net/whatsinagif/bits_and_bytes.html

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245