<p dir="auto">Hi! I'm using Glide in my project.</p>
<p dir="auto">Like some people, I had a problem，<code class="notranslate">How can I fetch a loaded image file from cache?</code></p>
<p dir="auto">I read the Glide api carefully ,and found <code class="notranslate">downloadOnly</code> ,but it can not solve my problem,<br>
Only need to get the picture cache, if not loaded do not need to download.</p>
<p dir="auto">And then I read the Glide api  again,found the <code class="notranslate">EngineKey</code> is key。<br>
finally，I replace the <code class="notranslate">EngineKey.java</code>,<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://cloud.githubusercontent.com/assets/7588760/23538285/3d100f58-000d-11e7-8595-a71495799c68.png"><img src="https://cloud.githubusercontent.com/assets/7588760/23538285/3d100f58-000d-11e7-8595-a71495799c68.png" alt="image" style="max-width: 100%;"></a></p>
<div class="highlight highlight-source-java notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="public class EngineKey implements Key {

    private static final String EMPTY_LOG_STRING = &quot;&quot;;
    private  String id;
    private  Key signature;
    private String stringKey;
    private int hashCode;
    private Key originalKey;

    public EngineKey(String id, Key signature, int width, int height, ResourceDecoder cacheDecoder,
                     ResourceDecoder sourceDecoder, Transformation transformation, ResourceEncoder encoder,
                     ResourceTranscoder transcoder, Encoder sourceEncoder){
        this(id,signature);
    }

    public EngineKey(String id, Key signature) {
        this.id = id;
        this.signature = signature;
    }

    public EngineKey(String id) {
        this(id, EmptySignature.obtain());
    }

    public Key getOriginalKey() {
        if (originalKey == null) {
            originalKey = new OriginalKey(id, signature);
        }
        return originalKey;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        EngineKey engineKey = (EngineKey) o;

        if (!id.equals(engineKey.id)) {
            return false;
        } else if (!signature.equals(engineKey.signature)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        if (hashCode == 0) {
            hashCode = id.hashCode();
            hashCode = 31 * hashCode + signature.hashCode();
        }
        return hashCode;
    }

    @Override
    public String toString() {
        if (stringKey == null) {
            stringKey = new StringBuilder()
                    .append(&quot;EngineKey{&quot;)
                    .append(id)
                    .append('+')
                    .append(signature)
                    .append('}')
                    .toString();
        }
        return stringKey;
    }

    @Override
    public void updateDiskCacheKey(MessageDigest messageDigest) throws UnsupportedEncodingException {
        signature.updateDiskCacheKey(messageDigest);
        messageDigest.update(id.getBytes(STRING_CHARSET_NAME));
    }
}"><pre class="notranslate"><span class="pl-k">public</span> <span class="pl-k">class</span> <span class="pl-smi">EngineKey</span> <span class="pl-k">implements</span> <span class="pl-smi">Key</span> {

    <span class="pl-k">private</span> <span class="pl-k">static</span> <span class="pl-k">final</span> <span class="pl-smi">String</span> <span class="pl-c1">EMPTY_LOG_STRING</span> = <span class="pl-s">""</span>;
    <span class="pl-k">private</span>  <span class="pl-smi">String</span> <span class="pl-s1">id</span>;
    <span class="pl-k">private</span>  <span class="pl-smi">Key</span> <span class="pl-s1">signature</span>;
    <span class="pl-k">private</span> <span class="pl-smi">String</span> <span class="pl-s1">stringKey</span>;
    <span class="pl-k">private</span> <span class="pl-smi">int</span> <span class="pl-s1">hashCode</span>;
    <span class="pl-k">private</span> <span class="pl-smi">Key</span> <span class="pl-s1">originalKey</span>;

    <span class="pl-k">public</span> <span class="pl-smi">EngineKey</span>(<span class="pl-smi">String</span> <span class="pl-s1">id</span>, <span class="pl-smi">Key</span> <span class="pl-s1">signature</span>, <span class="pl-smi">int</span> <span class="pl-s1">width</span>, <span class="pl-smi">int</span> <span class="pl-s1">height</span>, <span class="pl-smi">ResourceDecoder</span> <span class="pl-s1">cacheDecoder</span>,
                     <span class="pl-smi">ResourceDecoder</span> <span class="pl-s1">sourceDecoder</span>, <span class="pl-smi">Transformation</span> <span class="pl-s1">transformation</span>, <span class="pl-smi">ResourceEncoder</span> <span class="pl-s1">encoder</span>,
                     <span class="pl-smi">ResourceTranscoder</span> <span class="pl-s1">transcoder</span>, <span class="pl-smi">Encoder</span> <span class="pl-s1">sourceEncoder</span>){
        <span class="pl-smi">this</span>(<span class="pl-s1">id</span>,<span class="pl-s1">signature</span>);
    }

    <span class="pl-k">public</span> <span class="pl-smi">EngineKey</span>(<span class="pl-smi">String</span> <span class="pl-s1">id</span>, <span class="pl-smi">Key</span> <span class="pl-s1">signature</span>) {
        <span class="pl-smi">this</span>.<span class="pl-s1">id</span> = <span class="pl-s1">id</span>;
        <span class="pl-smi">this</span>.<span class="pl-s1">signature</span> = <span class="pl-s1">signature</span>;
    }

    <span class="pl-k">public</span> <span class="pl-smi">EngineKey</span>(<span class="pl-smi">String</span> <span class="pl-s1">id</span>) {
        <span class="pl-smi">this</span>(<span class="pl-s1">id</span>, <span class="pl-smi">EmptySignature</span>.<span class="pl-en">obtain</span>());
    }

    <span class="pl-k">public</span> <span class="pl-smi">Key</span> <span class="pl-en">getOriginalKey</span>() {
        <span class="pl-k">if</span> (<span class="pl-s1">originalKey</span> == <span class="pl-c1">null</span>) {
            <span class="pl-s1">originalKey</span> = <span class="pl-k">new</span> <span class="pl-smi">OriginalKey</span>(<span class="pl-s1">id</span>, <span class="pl-s1">signature</span>);
        }
        <span class="pl-k">return</span> <span class="pl-s1">originalKey</span>;
    }

    <span class="pl-c1">@</span><span class="pl-c1">Override</span>
    <span class="pl-k">public</span> <span class="pl-smi">boolean</span> <span class="pl-en">equals</span>(<span class="pl-smi">Object</span> <span class="pl-s1">o</span>) {
        <span class="pl-k">if</span> (<span class="pl-smi">this</span> == <span class="pl-s1">o</span>) {
            <span class="pl-k">return</span> <span class="pl-c1">true</span>;
        }
        <span class="pl-k">if</span> (<span class="pl-s1">o</span> == <span class="pl-c1">null</span> || <span class="pl-en">getClass</span>() != <span class="pl-s1">o</span>.<span class="pl-en">getClass</span>()) {
            <span class="pl-k">return</span> <span class="pl-c1">false</span>;
        }

        <span class="pl-smi">EngineKey</span> <span class="pl-s1">engineKey</span> = (<span class="pl-smi">EngineKey</span>) <span class="pl-s1">o</span>;

        <span class="pl-k">if</span> (!<span class="pl-s1">id</span>.<span class="pl-en">equals</span>(<span class="pl-s1">engineKey</span>.<span class="pl-s1">id</span>)) {
            <span class="pl-k">return</span> <span class="pl-c1">false</span>;
        } <span class="pl-k">else</span> <span class="pl-k">if</span> (!<span class="pl-s1">signature</span>.<span class="pl-en">equals</span>(<span class="pl-s1">engineKey</span>.<span class="pl-s1">signature</span>)) {
            <span class="pl-k">return</span> <span class="pl-c1">false</span>;
        }
        <span class="pl-k">return</span> <span class="pl-c1">true</span>;
    }

    <span class="pl-c1">@</span><span class="pl-c1">Override</span>
    <span class="pl-k">public</span> <span class="pl-smi">int</span> <span class="pl-en">hashCode</span>() {
        <span class="pl-k">if</span> (<span class="pl-s1">hashCode</span> == <span class="pl-c1">0</span>) {
            <span class="pl-s1">hashCode</span> = <span class="pl-s1">id</span>.<span class="pl-en">hashCode</span>();
            <span class="pl-s1">hashCode</span> = <span class="pl-c1">31</span> * <span class="pl-s1">hashCode</span> + <span class="pl-s1">signature</span>.<span class="pl-en">hashCode</span>();
        }
        <span class="pl-k">return</span> <span class="pl-s1">hashCode</span>;
    }

    <span class="pl-c1">@</span><span class="pl-c1">Override</span>
    <span class="pl-k">public</span> <span class="pl-smi">String</span> <span class="pl-en">toString</span>() {
        <span class="pl-k">if</span> (<span class="pl-s1">stringKey</span> == <span class="pl-c1">null</span>) {
            <span class="pl-s1">stringKey</span> = <span class="pl-k">new</span> <span class="pl-smi">StringBuilder</span>()
                    .<span class="pl-en">append</span>(<span class="pl-s">"EngineKey{"</span>)
                    .<span class="pl-en">append</span>(<span class="pl-s1">id</span>)
                    .<span class="pl-en">append</span>(<span class="pl-s">'+'</span>)
                    .<span class="pl-en">append</span>(<span class="pl-s1">signature</span>)
                    .<span class="pl-en">append</span>(<span class="pl-s">'}'</span>)
                    .<span class="pl-en">toString</span>();
        }
        <span class="pl-k">return</span> <span class="pl-s1">stringKey</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">updateDiskCacheKey</span>(<span class="pl-smi">MessageDigest</span> <span class="pl-s1">messageDigest</span>) <span class="pl-k">throws</span> <span class="pl-smi">UnsupportedEncodingException</span> {
        <span class="pl-s1">signature</span>.<span class="pl-en">updateDiskCacheKey</span>(<span class="pl-s1">messageDigest</span>);
        <span class="pl-s1">messageDigest</span>.<span class="pl-en">update</span>(<span class="pl-s1">id</span>.<span class="pl-en">getBytes</span>(<span class="pl-c1">STRING_CHARSET_NAME</span>));
    }
}</pre></div>
<p dir="auto">use like that:</p>
<div class="highlight highlight-source-java notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="String url = &quot;http://xxxxxx&quot;;
ExternalCacheDiskCacheFactory factory = new ExternalCacheDiskCacheFactory(activity);
File file = factory.build().get(new EngineKey(url));"><pre class="notranslate"><span class="pl-smi">String</span> <span class="pl-s1">url</span> = <span class="pl-s">"http://xxxxxx"</span>;
<span class="pl-smi">ExternalCacheDiskCacheFactory</span> <span class="pl-s1">factory</span> = <span class="pl-k">new</span> <span class="pl-smi">ExternalCacheDiskCacheFactory</span>(<span class="pl-s1">activity</span>);
<span class="pl-smi">File</span> <span class="pl-s1">file</span> = <span class="pl-s1">factory</span>.<span class="pl-en">build</span>().<span class="pl-en">get</span>(<span class="pl-k">new</span> <span class="pl-smi">EngineKey</span>(<span class="pl-s1">url</span>));</pre></div>
<p dir="auto">Hi,now I want to migrate my app from universalimageloader to glide. But I have a question is that I can‘t judge a image is already cached in disk。</p>
<p dir="auto">There is my case:<br>
There are many different resources and different URL for one image. Different network environments load different resources。For example，When I was in the 3G network，load the low quality image resource and the URL is "http://www.xxxxx_3g.png"。When I was in the WIFI network，load the high quality image resource and the URL is "http://www.yyyyyy_wifi.png"，There are two different URLs。</p>
<p dir="auto">BUT! When I was in the 3G network，check the  high quality image resource first。If the high quality image resource is already cached, load the  high quality image resource URL from disk cache. Otherwise  load "http://www.xxxxx_3g.png"。</p>
<div class="highlight highlight-source-java notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="if (isCache(&quot;http://www.yyyyyy_wifi.png&quot;) {
    Glide.with(...)
        .load(&quot;http://www.yyyyyy_wifi.png&quot;)
        .into(...);
} else {
    Glide.with(...)
        .load(&quot;http://www.xxxxx_3g.png&quot;)
        .into(...);
}

public boolean isCache(String url) {
    // what should I do ???
}"><pre class="notranslate"><span class="pl-k">if</span> (<span class="pl-en">isCache</span>(<span class="pl-s">"http://www.yyyyyy_wifi.png"</span>) {
    <span class="pl-smi">Glide</span>.<span class="pl-en">with</span>(...)
        .<span class="pl-en">load</span>(<span class="pl-s">"http://www.yyyyyy_wifi.png"</span>)
        .<span class="pl-en">into</span>(...);
} <span class="pl-k">else</span> {
    <span class="pl-smi">Glide</span>.<span class="pl-en">with</span>(...)
        .<span class="pl-en">load</span>(<span class="pl-s">"http://www.xxxxx_3g.png"</span>)
        .<span class="pl-en">into</span>(...);
}

<span class="pl-k">public</span> <span class="pl-smi">boolean</span> <span class="pl-s1">isCache</span>(<span class="pl-smi">String</span> <span class="pl-s1">url</span>) {
    <span class="pl-c">// what should I do ???</span>
}</pre></div>
<p dir="auto">So,How can I judge the high quality image resource(URL is "http://www.yyyyyy_wifi.png") is already cached in disk synchronously?</p>
<p dir="auto">Thank you very much。</p>