Use the Metadata-Extractor library to read the meta-information of image files. This piece of code can be used to read the GifControlDirectory for each image (frame) in the GIF file.
/** * Get time length of GIF file in milliseconds. * * @return time length of gif in ms. */ public int getGifAnimatedTimeLength(String imagePath) { Metadata metadata = ImageMetadataReader.readMetadata(imagePath); List<GifControlDirectory> gifControlDirectories = (List<GifControlDirectory>) metadata.getDirectoriesOfType(GifControlDirectory.class); int timeLength = 0; if (gifControlDirectories.size() == 1) { // Do not read delay of static GIF files with single frame. } else if (gifControlDirectories.size() >= 1) { for (GifControlDirectory gifControlDirectory : gifControlDirectories) { try { if (gifControlDirectory.hasTagName(GifControlDirectory.TAG_DELAY)) { timeLength += gifControlDirectory.getInt(GifControlDirectory.TAG_DELAY); } } catch (MetadataException e) { e.printStackTrace(); } } // Unit of time is 10 milliseconds in GIF. timeLength *= 10; } return timeLength; }
Please note that even if the GIF file has several frames, it is possible that each frame may have a delay of 0 or without TAG_DELAY metadata. In addition, this article describes another problem of how different platforms use the delay value.
source share