title: Glide - Save GIF to File
body: <p>Sidenote - although some of my examples are in Kotlin, answers in either Java or Kotlin are helpful.</p>
  
  <p>I'm creating a GIF keyboard using the <a href="https://developer.android.com/guide/topics/text/image-keyboard" rel="nofollow noreferrer">Commit Content API</a>. To do this, I need to be able to create <code>.gif</code> files.</p>
  
  <p>I'm using Glide to fetch the images from the internet and display them in a list inside of a <code>recyclerview</code>.</p>
  
  <p>For each image Glide downloads, I have to create a <code>File</code> object with the data of that image.</p>
  
  <p>I see two methods that could accomplish this objective.</p>
  
  <p>1] Create a listener that overrides <code>onResourceReady</code> like so. The problem with this method is that I don't know how to convert a <code>GifDrawable</code> to a <code>.gif</code> file because all the Stack Overflow posts talk about how to convert a <code>Drawable</code> to a file:</p>
  
  <pre><code>    Glide.with(context)
              .asGif()
              .load(items[position])
              .listener(object : RequestListener&lt;GifDrawable&gt; {
                  override fun onLoadFailed(e: GlideException?, model: Any?, target: Target&lt;GifDrawable&gt;?, isFirstResource: Boolean): Boolean {
                      Log.d("VED-APP","Glide Load Failed")
                      return false
                  }
  
                  override fun onResourceReady(resource: GifDrawable?, model: Any?, target: Target&lt;GifDrawable&gt;?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                      //I need code to convert the GifDrawable to a File
  
                      return false
                  }
  
              })
              .into(holder.imageView)
  </code></pre>
  
  <p>2] Load the image to a <code>ByteArray</code> or something similar. Example Java code, taken from the post <a href="https://stackoverflow.com/questions/40129543/glide-download-and-resize-gif-into-a-file?rq=1">Glide - download and resize GIF into a file</a>, could look like this. The problem with this code is that </p>
  
  <p>A] <code>.into(width,height)</code> is deprecated </p>
  
  <p>B] there is no <code>.toBytes</code> method </p>
  
  <p>C] I don't want to specify the dimensions of my GIF - I'd just like to use the original size. (Or, at the very least, I'd like to preserve the aspect ratio)</p>
  
  <pre><code>byte[] bytes = Glide.with(context)
                      .load(items[position])
                      .asGif()
                      .toBytes()
                      .into(250, 250)
                      .get();
  file = new File(fileName);
  
  FileOutputStream fileWriter = new FileOutputStream(file);
  fileWriter.write(bytes);
  fileWriter.flush();
  fileWriter.close();
  </code></pre>
  
  <p>Can someone help me improve either of these two methods, or suggest a new way to convert a GIF to a file with Glide? </p>