<p dir="auto">I want to decode gif by android-gif-drawable, but load picture with glide.<br>
<a href="https://github.com/koral--/android-gif-drawable-eclipse-sample">android-gif-drawable</a> can pass byte array to decode gif and my code is:</p>
<div class="highlight highlight-source-java notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="public void test1(final GifImageView gifImageView) throws IOException {
        // String path1 =
        // &quot;http://img.newyx.net/news_img/201306/20/1371714170_1812223777.gif&quot;;
        // test gif url
        String path = &quot;http://cdn.duitang.com/uploads/item/201311/20/20131120213622_mJCUy.thumb.600_0.gif&quot;;
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5 * 1000);
        conn.setRequestMethod(&quot;GET&quot;);
        if (conn.getResponseCode() == HttpStatus.SC_OK) {
            InputStream sourceIs = conn.getInputStream();
            byte[] b = InputStreamTOByte(sourceIs);
            final GifDrawable gifFromStream = new GifDrawable(b);
            gifImageView.post(new Runnable() {
                @Override
                public void run() {
                    gifImageView.setImageDrawable(gifFromStream);
                }
            });
        }
    }

    public static byte[] InputStreamTOByte(InputStream in) throws IOException {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        int BUFFER_SIZE = 1024;
        byte[] data = new byte[BUFFER_SIZE];
        int count = -1;
        while ((count = in.read(data, 0, BUFFER_SIZE)) != -1) {
            outStream.write(data, 0, count);
        }
        data = null;
        return outStream.toByteArray();
    }"><pre class="notranslate"><span class="pl-k">public</span> <span class="pl-smi">void</span> <span class="pl-s1">test1</span>(<span class="pl-k">final</span> <span class="pl-smi">GifImageView</span> <span class="pl-s1">gifImageView</span>) <span class="pl-k">throws</span> <span class="pl-s1">IOException</span> {
        <span class="pl-c">// String path1 =</span>
        <span class="pl-c">// "http://img.newyx.net/news_img/201306/20/1371714170_1812223777.gif";</span>
        <span class="pl-c">// test gif url</span>
        <span class="pl-smi">String</span> <span class="pl-s1">path</span> = <span class="pl-s">"http://cdn.duitang.com/uploads/item/201311/20/20131120213622_mJCUy.thumb.600_0.gif"</span>;
        <span class="pl-smi">URL</span> <span class="pl-s1">url</span> = <span class="pl-k">new</span> <span class="pl-smi">URL</span>(<span class="pl-s1">path</span>);
        <span class="pl-smi">HttpURLConnection</span> <span class="pl-s1">conn</span> = (<span class="pl-smi">HttpURLConnection</span>) <span class="pl-s1">url</span>.<span class="pl-en">openConnection</span>();
        <span class="pl-s1">conn</span>.<span class="pl-en">setConnectTimeout</span>(<span class="pl-c1">5</span> * <span class="pl-c1">1000</span>);
        <span class="pl-s1">conn</span>.<span class="pl-en">setRequestMethod</span>(<span class="pl-s">"GET"</span>);
        <span class="pl-k">if</span> (<span class="pl-s1">conn</span>.<span class="pl-en">getResponseCode</span>() == <span class="pl-smi">HttpStatus</span>.<span class="pl-c1">SC_OK</span>) {
            <span class="pl-smi">InputStream</span> <span class="pl-s1">sourceIs</span> = <span class="pl-s1">conn</span>.<span class="pl-en">getInputStream</span>();
            <span class="pl-smi">byte</span>[] <span class="pl-s1">b</span> = <span class="pl-en">InputStreamTOByte</span>(<span class="pl-s1">sourceIs</span>);
            <span class="pl-k">final</span> <span class="pl-smi">GifDrawable</span> <span class="pl-s1">gifFromStream</span> = <span class="pl-k">new</span> <span class="pl-smi">GifDrawable</span>(<span class="pl-s1">b</span>);
            <span class="pl-s1">gifImageView</span>.<span class="pl-en">post</span>(<span class="pl-k">new</span> <span class="pl-smi">Runnable</span>() {
                <span class="pl-c1">@</span><span class="pl-c1">Override</span>
                <span class="pl-k">public</span> <span class="pl-smi">void</span> <span class="pl-en">run</span>() {
                    <span class="pl-s1">gifImageView</span>.<span class="pl-en">setImageDrawable</span>(<span class="pl-s1">gifFromStream</span>);
                }
            });
        }
    }

    <span class="pl-k">public</span> <span class="pl-k">static</span> <span class="pl-smi">byte</span>[] <span class="pl-s1">InputStreamTOByte</span>(<span class="pl-smi">InputStream</span> <span class="pl-s1">in</span>) <span class="pl-k">throws</span> <span class="pl-s1">IOException</span> {
        <span class="pl-smi">ByteArrayOutputStream</span> <span class="pl-s1">outStream</span> = <span class="pl-k">new</span> <span class="pl-smi">ByteArrayOutputStream</span>();
        <span class="pl-smi">int</span> <span class="pl-c1">BUFFER_SIZE</span> = <span class="pl-c1">1024</span>;
        <span class="pl-smi">byte</span>[] <span class="pl-s1">data</span> = <span class="pl-k">new</span> <span class="pl-smi">byte</span>[<span class="pl-c1">BUFFER_SIZE</span>];
        <span class="pl-smi">int</span> <span class="pl-s1">count</span> = -<span class="pl-c1">1</span>;
        <span class="pl-k">while</span> ((<span class="pl-s1">count</span> = <span class="pl-s1">in</span>.<span class="pl-en">read</span>(<span class="pl-s1">data</span>, <span class="pl-c1">0</span>, <span class="pl-c1">BUFFER_SIZE</span>)) != -<span class="pl-c1">1</span>) {
            <span class="pl-s1">outStream</span>.<span class="pl-en">write</span>(<span class="pl-s1">data</span>, <span class="pl-c1">0</span>, <span class="pl-s1">count</span>);
        }
        <span class="pl-s1">data</span> = <span class="pl-c1">null</span>;
        <span class="pl-k">return</span> <span class="pl-s1">outStream</span>.<span class="pl-en">toByteArray</span>();
    }</pre></div>
<p dir="auto">and I use path url to test, it can display normal. Then I use glide to decode the same path url.<br>
My code is this:</p>
<div class="highlight highlight-source-java notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// first
// Glide.with(MainActivity.this).load(path).into(imageView);
// second
Glide.with(MainActivity.this)
    .load(path)
    .asGif()
    .toBytes()
    .into(new SimpleTarget&lt;byte[]&gt;() {
        @Override
        public void onResourceReady(final byte[] resource,
                GlideAnimation&lt;? super byte[]&gt; glideAnimation) {
            GifDrawable gifFromStream;
            try {
                gifFromStream = new GifDrawable(resource);
                gifImageView.setImageDrawable(gifFromStream);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });"><pre class="notranslate"><span class="pl-c">// first</span>
<span class="pl-c">// Glide.with(MainActivity.this).load(path).into(imageView);</span>
<span class="pl-c">// second</span>
<span class="pl-smi">Glide</span>.<span class="pl-en">with</span>(<span class="pl-smi">MainActivity</span>.<span class="pl-smi">this</span>)
    .<span class="pl-en">load</span>(<span class="pl-s1">path</span>)
    .<span class="pl-en">asGif</span>()
    .<span class="pl-en">toBytes</span>()
    .<span class="pl-en">into</span>(<span class="pl-k">new</span> <span class="pl-smi">SimpleTarget</span>&lt;<span class="pl-smi">byte</span>[]&gt;() {
        <span class="pl-c1">@</span><span class="pl-c1">Override</span>
        <span class="pl-k">public</span> <span class="pl-smi">void</span> <span class="pl-en">onResourceReady</span>(<span class="pl-k">final</span> <span class="pl-smi">byte</span>[] <span class="pl-s1">resource</span>,
                <span class="pl-smi">GlideAnimation</span>&lt;? <span class="pl-en">super</span> <span class="pl-smi">byte</span>[]&gt; <span class="pl-s1">glideAnimation</span>) {
            <span class="pl-smi">GifDrawable</span> <span class="pl-s1">gifFromStream</span>;
            <span class="pl-k">try</span> {
                <span class="pl-s1">gifFromStream</span> = <span class="pl-k">new</span> <span class="pl-smi">GifDrawable</span>(<span class="pl-s1">resource</span>);
                <span class="pl-s1">gifImageView</span>.<span class="pl-en">setImageDrawable</span>(<span class="pl-s1">gifFromStream</span>);
            } <span class="pl-k">catch</span> (<span class="pl-smi">IOException</span> <span class="pl-s1">e</span>) {
                <span class="pl-s1">e</span>.<span class="pl-en">printStackTrace</span>();
            }
        }
    });</pre></div>
<p dir="auto">No matter the first way or the second way, pictures can't display.</p>
<h2 dir="auto">Idea</h2>
<div class="highlight highlight-source-java notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="Glide.with(this).load(bitmap).into(imageView);
Glide.with(this).load(drawable).into(imageView);"><pre class="notranslate"><span class="pl-smi">Glide</span>.<span class="pl-en">with</span>(<span class="pl-smi">this</span>).<span class="pl-en">load</span>(<span class="pl-s1">bitmap</span>).<span class="pl-en">into</span>(<span class="pl-s1">imageView</span>);
<span class="pl-smi">Glide</span>.<span class="pl-en">with</span>(<span class="pl-smi">this</span>).<span class="pl-en">load</span>(<span class="pl-s1">drawable</span>).<span class="pl-en">into</span>(<span class="pl-s1">imageView</span>);</pre></div>
<p dir="auto">This looks weird compared to <code class="notranslate">ImageView.setImageBitmap(bitmap)</code>, but if you need animation and error handling (null bitmap), the above could become an easy to use, fire and forget, go-to line instead of duplicating code.<br>
Note: you've already made a wrapper of <code class="notranslate">ImageView#setImageResource</code> and <code class="notranslate">ImageView#setImageUri</code>.</p>
<p dir="auto">Currently they throw</p>
<blockquote>
<p dir="auto">Caused by: java.lang.IllegalArgumentException: Unknown type android.graphics.Bitmap@44759ab8. You must provide a Model of a type for which there is a registered ModelLoader, if you are using a custom model, you must first call Glide#register with a ModelLoaderFactory for your custom model class.<br>
<code class="notranslate">at com.bumptech.glide.RequestManager.loadGeneric(RequestManager.java:382)</code><br>
<code class="notranslate">at com.bumptech.glide.RequestManager.load(RequestManager.java:374)</code></p>
</blockquote>
<h2 dir="auto">Context</h2>
<p dir="auto">This came up when I was trying to display a captured image using Glide (oversimplified code):</p>
<div class="highlight highlight-source-java notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), Activity.RESULT_FIRST_USER);

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == REQUEST_CODE_GET_PICTURE &amp;&amp; resultCode == Activity.RESULT_OK) {
        Bitmap bitmap = (Bitmap)data.getExtras().get(&quot;data&quot;);
        Glide.with(this).load(bitmap)/*...*/.into(image);"><pre class="notranslate"><span class="pl-en">startActivityForResult</span>(<span class="pl-k">new</span> <span class="pl-smi">Intent</span>(<span class="pl-smi">MediaStore</span>.<span class="pl-c1">ACTION_IMAGE_CAPTURE</span>), <span class="pl-smi">Activity</span>.<span class="pl-c1">RESULT_FIRST_USER</span>);

<span class="pl-k">public</span> <span class="pl-smi">void</span> <span class="pl-s1">onActivityResult</span>(<span class="pl-smi">int</span> <span class="pl-s1">requestCode</span>, <span class="pl-smi">int</span> <span class="pl-s1">resultCode</span>, <span class="pl-smi">Intent</span> <span class="pl-s1">data</span>) {
    <span class="pl-k">if</span>(<span class="pl-s1">requestCode</span> == <span class="pl-c1">REQUEST_CODE_GET_PICTURE</span> &amp;&amp; <span class="pl-s1">resultCode</span> == <span class="pl-smi">Activity</span>.<span class="pl-c1">RESULT_OK</span>) {
        <span class="pl-smi">Bitmap</span> <span class="pl-s1">bitmap</span> = (<span class="pl-smi">Bitmap</span>)<span class="pl-s1">data</span>.<span class="pl-en">getExtras</span>().<span class="pl-en">get</span>(<span class="pl-s">"data"</span>);
        <span class="pl-smi">Glide</span>.<span class="pl-en">with</span>(<span class="pl-smi">this</span>).<span class="pl-en">load</span>(<span class="pl-s1">bitmap</span>)<span class="pl-c">/*...*/</span>.<span class="pl-en">into</span>(<span class="pl-s1">image</span>);</pre></div>