Android Gif File Check isGif(String filePath)

Here you can find the source of isGif(String filePath)

Description

is Gif

License

Apache License

Declaration

public static boolean isGif(String filePath) 

Method Source Code

//License from project: Apache License 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main{
    private static final Logger logger = LoggerFactory
            .getLogger(FileUtil.class);
    public static boolean isGif(String filePath) {
        boolean isGif = false;
        if (StringUtil.isEmpty(filePath)) {
            return isGif;
        }/*from  w ww. ja  v  a  2 s  .  c o  m*/

        File file = new File(filePath);
        if (file.exists() && file.isFile()) {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);
                StringBuilder head = new StringBuilder("");
                for (int i = 0; i < 6; i++) {
                    head.append((char) fis.read());
                }
                if (head.indexOf("GIF") == 0) {
                    isGif = true;
                }
            } catch (FileNotFoundException e) {
                logger.error("isGif", e);
            } catch (IOException e) {
                logger.error("isGif", e);
            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                    }
                }
            }
        }

        return isGif;
    }
}