title: Converting a video file to gif
body: <p>I've been trying to figure out how to convert a video file to a gif in android. I found a class online, the <code>AnimatedGifEncoder</code> class (<a href="https://github.com/nbadal/android-gif-encoder/blob/master/GifEncoder.java" rel="nofollow noreferrer">https://github.com/nbadal/android-gif-encoder/blob/master/GifEncoder.java</a>). But it doesn't really seem to work. I wrapped the video to gif conversion process in an <code>AsnycTask</code>. The code is shown below:</p>
  
  <pre><code>@Override
  protected String doInBackground(File[] files) {
      MediaMetadataRetriever mmr = new MediaMetadataRetriever();
      try {
          mmr.setDataSource(files[0].getPath());
      } catch (Exception e){
          Log.i("VMPF", e.toString());
      }
      //int framesRate = Integer.parseInt(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_CAPTURE_FRAMERATE));
      int duration = Integer.parseInt(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
      Log.i("gifduration","" + duration);
      //Log.i("giffps","" + framesRate);
      int millisPerframe = 1000/24;
      int timeAt = 0;
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      AnimatedGifEncoder encoder = new AnimatedGifEncoder();
      encoder.setDelay(0);
      encoder.setRepeat(100);
      encoder.start(bos);
      while (timeAt &lt; duration){
          encoder.addFrame(mmr.getFrameAtTime(timeAt));
          timeAt += millisPerframe;
          Log.i("wenfhluwhru", timeAt+"");
      }
      encoder.finish();
      Log.i("wenfhluwhru", "loop finished");
      FileOutputStream outStream = null;
      try {
          outStream = new FileOutputStream(path);
          outStream.write(bos.toByteArray());
          outStream.close();
          Log.i("wenfhluwhru", "outstream closed");
          return path;
      } catch(Exception e) {
          e.printStackTrace();
      }
      return null;
  }
  </code></pre>
  
  <p>Is there something wrong with my code (I assume they're nothing wrong with the <code>AnimatedGifEncoder</code> class)? Also I thought about converting a file to a <code>byte[]</code> and just submitting the <code>byte[]</code> into the <code>GifImageView</code> via <code>Glide</code> but I'm willing to guess that the byte[] for a video is different from that of a gif file (if I'm wrong, please let me know)?</p>