<p dir="auto"><strong>Glide Version</strong>: 4.4</p>

<p dir="auto"><strong>Integration libraries</strong>:</p>

<p dir="auto"><strong>Device/Android Version</strong>:</p>

<p dir="auto"><strong>Issue details / Repro steps / Use case background</strong>:</p>

<p dir="auto"><strong>Glide load line / <code class="notranslate">GlideModule</code> (if any) / list Adapter code (if any)</strong>:</p>
<div class="highlight highlight-source-java notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="Glide.with..."><pre class="notranslate"><span class="pl-smi">Glide</span>.<span class="pl-smi">with</span>...</pre></div>

<p dir="auto"><strong>Layout XML</strong>:</p>
<div class="highlight highlight-text-xml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&lt;FrameLayout xmlns:android=&quot;..."><pre class="notranslate">&lt;<span class="pl-ent">FrameLayout</span> <span class="pl-e">xmlns</span><span class="pl-e">:</span><span class="pl-e">android</span>=<span class="pl-s"><span class="pl-pds">"</span>...</span></pre></div>

<p dir="auto"><strong>Stack trace / LogCat</strong>:</p>
<div class="highlight highlight-source-ruby notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="java.lang.IllegalStateException: Already released
at com.bumptech.glide.util.pool.StateVerifier$DefaultStateVerifier.throwIfRecycled(StateVerifier.java:44)
at com.bumptech.glide.request.SingleRequest.onResourceReady(SingleRequest.java:518)
at com.bumptech.glide.load.engine.EngineJob.handleResultOnMainThread(EngineJob.java:217)
at com.bumptech.glide.load.engine.EngineJob$MainThreadCallback.handleMessage(EngineJob.java:322)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6247)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)"><pre class="notranslate"><span class="pl-en">java</span><span class="pl-kos">.</span><span class="pl-en">lang</span><span class="pl-kos">.</span><span class="pl-en">IllegalStateException</span>: <span class="pl-en">Already</span> <span class="pl-en">released</span>
<span class="pl-en">at</span> <span class="pl-en">com</span><span class="pl-kos">.</span><span class="pl-en">bumptech</span><span class="pl-kos">.</span><span class="pl-en">glide</span><span class="pl-kos">.</span><span class="pl-en">util</span><span class="pl-kos">.</span><span class="pl-en">pool</span><span class="pl-kos">.</span><span class="pl-en">StateVerifier</span>$DefaultStateVerifier<span class="pl-kos">.</span><span class="pl-en">throwIfRecycled</span><span class="pl-kos">(</span><span class="pl-v">StateVerifier</span><span class="pl-kos">.</span><span class="pl-en">java</span><span class="pl-pds">:44</span><span class="pl-kos">)</span>
<span class="pl-en">at</span> <span class="pl-en">com</span><span class="pl-kos">.</span><span class="pl-en">bumptech</span><span class="pl-kos">.</span><span class="pl-en">glide</span><span class="pl-kos">.</span><span class="pl-en">request</span><span class="pl-kos">.</span><span class="pl-en">SingleRequest</span><span class="pl-kos">.</span><span class="pl-en">onResourceReady</span><span class="pl-kos">(</span><span class="pl-v">SingleRequest</span><span class="pl-kos">.</span><span class="pl-en">java</span><span class="pl-pds">:518</span><span class="pl-kos">)</span>
<span class="pl-en">at</span> <span class="pl-en">com</span><span class="pl-kos">.</span><span class="pl-en">bumptech</span><span class="pl-kos">.</span><span class="pl-en">glide</span><span class="pl-kos">.</span><span class="pl-en">load</span><span class="pl-kos">.</span><span class="pl-en">engine</span><span class="pl-kos">.</span><span class="pl-en">EngineJob</span><span class="pl-kos">.</span><span class="pl-en">handleResultOnMainThread</span><span class="pl-kos">(</span><span class="pl-v">EngineJob</span><span class="pl-kos">.</span><span class="pl-en">java</span><span class="pl-pds">:217</span><span class="pl-kos">)</span>
<span class="pl-en">at</span> <span class="pl-en">com</span><span class="pl-kos">.</span><span class="pl-en">bumptech</span><span class="pl-kos">.</span><span class="pl-en">glide</span><span class="pl-kos">.</span><span class="pl-en">load</span><span class="pl-kos">.</span><span class="pl-en">engine</span><span class="pl-kos">.</span><span class="pl-en">EngineJob</span>$MainThreadCallback<span class="pl-kos">.</span><span class="pl-en">handleMessage</span><span class="pl-kos">(</span><span class="pl-v">EngineJob</span><span class="pl-kos">.</span><span class="pl-en">java</span><span class="pl-pds">:322</span><span class="pl-kos">)</span>
<span class="pl-en">at</span> <span class="pl-en">android</span><span class="pl-kos">.</span><span class="pl-en">os</span><span class="pl-kos">.</span><span class="pl-en">Handler</span><span class="pl-kos">.</span><span class="pl-en">dispatchMessage</span><span class="pl-kos">(</span><span class="pl-v">Handler</span><span class="pl-kos">.</span><span class="pl-en">java</span><span class="pl-pds">:98</span><span class="pl-kos">)</span>
<span class="pl-en">at</span> <span class="pl-en">android</span><span class="pl-kos">.</span><span class="pl-en">os</span><span class="pl-kos">.</span><span class="pl-en">Looper</span><span class="pl-kos">.</span><span class="pl-en">loop</span><span class="pl-kos">(</span><span class="pl-v">Looper</span><span class="pl-kos">.</span><span class="pl-en">java</span><span class="pl-pds">:154</span><span class="pl-kos">)</span>
<span class="pl-en">at</span> <span class="pl-en">android</span><span class="pl-kos">.</span><span class="pl-en">app</span><span class="pl-kos">.</span><span class="pl-en">ActivityThread</span><span class="pl-kos">.</span><span class="pl-en">main</span><span class="pl-kos">(</span><span class="pl-v">ActivityThread</span><span class="pl-kos">.</span><span class="pl-en">java</span><span class="pl-pds">:6247</span><span class="pl-kos">)</span>
<span class="pl-en">at</span> <span class="pl-en">java</span><span class="pl-kos">.</span><span class="pl-en">lang</span><span class="pl-kos">.</span><span class="pl-en">reflect</span><span class="pl-kos">.</span><span class="pl-en">Method</span><span class="pl-kos">.</span><span class="pl-en">invoke</span><span class="pl-kos">(</span><span class="pl-en">Native</span> <span class="pl-v">Method</span><span class="pl-kos">)</span>
<span class="pl-en">at</span> <span class="pl-en">com</span><span class="pl-kos">.</span><span class="pl-en">android</span><span class="pl-kos">.</span><span class="pl-en">internal</span><span class="pl-kos">.</span><span class="pl-en">os</span><span class="pl-kos">.</span><span class="pl-en">ZygoteInit</span>$MethodAndArgsCaller<span class="pl-kos">.</span><span class="pl-en">run</span><span class="pl-kos">(</span><span class="pl-v">ZygoteInit</span><span class="pl-kos">.</span><span class="pl-en">java</span><span class="pl-pds">:872</span><span class="pl-kos">)</span>
<span class="pl-en">at</span> <span class="pl-en">com</span><span class="pl-kos">.</span><span class="pl-en">android</span><span class="pl-kos">.</span><span class="pl-en">internal</span><span class="pl-kos">.</span><span class="pl-en">os</span><span class="pl-kos">.</span><span class="pl-en">ZygoteInit</span><span class="pl-kos">.</span><span class="pl-en">main</span><span class="pl-kos">(</span><span class="pl-v">ZygoteInit</span><span class="pl-kos">.</span><span class="pl-en">java</span><span class="pl-pds">:762</span><span class="pl-kos">)</span></pre></div>
<p dir="auto">How can I catch this exception instead of letting it crash my app? Thanks.</p>
<p dir="auto">Hey,</p>
<p dir="auto">I use Glide to download a GIF and insert it into PhotoView. Works fine, but I want to be able to save this GIF to my local storage. I have searched through issues over here, but none was viable for me.<br>
I was trying to use this part of code to get this done:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="override fun getDrawable() : Drawable? {
    photoView.image.drawable?.apply {
        return this
    }
    return null
}

override fun saveImage() {
    val drawable = getDrawable()
    if (drawable == null || !checkForWriteReadPermission()) {
        return
    }
    if (drawable is BitmapDrawable) {
        val bitmap = drawable.bitmap
        val file = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), &quot;wykopmobilny&quot;)
        if (!file.exists()) file.mkdirs()

        val pictureFile = File(&quot;&quot;&quot;${file.absoluteFile}/${photoView.url.substringAfterLast('/')}&quot;&quot;&quot;)
        val fos = FileOutputStream(pictureFile)
        bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos)
        fos.close()
        addImageToGallery(pictureFile.absolutePath, context)
        showToastMessage(&quot;Zapisano obraz&quot;)
    }
    else if (drawable is GifDrawable) {
        val byteBuffer = drawable.buffer
        val file = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), &quot;wykopmobilny&quot;)
        if (!file.exists()) file.mkdirs()
        val gifFile = File(&quot;&quot;&quot;${file.absoluteFile}/${photoView.url.substringAfterLast('/')}&quot;&quot;&quot;)

        val output = FileOutputStream(gifFile)
        val bytes = ByteArray(byteBuffer.position())
        byteBuffer.get(bytes)
        output.write(bytes, 0, bytes.size)
        output.close()
		//val fileChannel = FileOutputStream(gifFile).channel
		//fileChannel.write(byteBuffer)
		//fileChannel.close()
        showToastMessage(&quot;Zapisano GIFa&quot;)
    }
}"><pre class="notranslate"><code class="notranslate">override fun getDrawable() : Drawable? {
    photoView.image.drawable?.apply {
        return this
    }
    return null
}

override fun saveImage() {
    val drawable = getDrawable()
    if (drawable == null || !checkForWriteReadPermission()) {
        return
    }
    if (drawable is BitmapDrawable) {
        val bitmap = drawable.bitmap
        val file = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "wykopmobilny")
        if (!file.exists()) file.mkdirs()

        val pictureFile = File("""${file.absoluteFile}/${photoView.url.substringAfterLast('/')}""")
        val fos = FileOutputStream(pictureFile)
        bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos)
        fos.close()
        addImageToGallery(pictureFile.absolutePath, context)
        showToastMessage("Zapisano obraz")
    }
    else if (drawable is GifDrawable) {
        val byteBuffer = drawable.buffer
        val file = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "wykopmobilny")
        if (!file.exists()) file.mkdirs()
        val gifFile = File("""${file.absoluteFile}/${photoView.url.substringAfterLast('/')}""")

        val output = FileOutputStream(gifFile)
        val bytes = ByteArray(byteBuffer.position())
        byteBuffer.get(bytes)
        output.write(bytes, 0, bytes.size)
        output.close()
		//val fileChannel = FileOutputStream(gifFile).channel
		//fileChannel.write(byteBuffer)
		//fileChannel.close()
        showToastMessage("Zapisano GIFa")
    }
}
</code></pre></div>
<p dir="auto">and it actually creates a file, but it is broken and cannot be open. For example the file size is 4.19, but the GIF size when downloading was 4.59, so it looks like it is missing some part of it.</p>
<p dir="auto">Glide version I use is 4.5 and the Widget is <a href="https://github.com/chrisbanes/PhotoView">https://github.com/chrisbanes/PhotoView</a></p>
<p dir="auto">PS. Saving static image works fine.</p>