admin
2020-08-12 cefe2a41db4a275fb1e940a902cb156f1ed68d80
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * Copyright © 2012-2013 LiuZhongnan. All rights reserved.
 * 
 * Email:qq81595157@126.com
 * 
 * PROPRIETARY/CONFIDENTIAL.
 */
 
package com.youku.service.download;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
 
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
 
import com.baseproject.utils.Logger;
import com.baseproject.utils.Util;
 
/**
 * AsyncImageLoader.图片缓冲
 * 
 * @author 刘仲男 qq81595157@126.com
 * @version v3.5
 * @created time 2012-11-5 下午1:16:02
 */
public class AsyncImageLoader {
    private static final String TAG = "AsyncImageLoader";
    private static AsyncImageLoader instance = null;
    // 图片软引用
    private HashMap<String, SoftReference<Drawable>> imageCache;
 
    // private View view;
    //
    // public AsyncImageLoader(ListView listView) {
    // this();
    // view = listView;
    // }
    //
    // public AsyncImageLoader(GridView gridView) {
    // this();
    // view = gridView;
    // }
 
    public synchronized static AsyncImageLoader getInstance() {
        if (instance == null)
            instance = new AsyncImageLoader();
        return instance;
    }
 
    private AsyncImageLoader() {
        imageCache = new HashMap<String, SoftReference<Drawable>>();
    }
 
    public Drawable loadDrawable(final ImageView imageView, final String url,
            final String pathName, final DownloadInfo info) {
        // imageView.setTag(pathName);
        if (imageCache.containsKey(pathName)) {
            Drawable drawable = imageCache.get(pathName).get();
            if (drawable != null)
                return drawable;
        }
        Drawable d = null;
        try {
            d = Drawable.createFromPath(pathName);
        } catch (OutOfMemoryError e) {
            // this.getImageCache().clearCaches();
            System.gc();
            Logger.e(TAG, "AsyncImageLoader#loadDrawable()", e);
        }
        if (d != null) {
            imageCache.put(pathName, new SoftReference<Drawable>(d));
            return d;
        }
 
        // final Handler handler = new Handler() {
        // public void handleMessage(Message message) {
        // ImageView imageView = (ImageView) view
        // .findViewWithTag(pathName);
        // if (imageView != null) {
        // imageView.setImageDrawable((Drawable) message.obj);
        // }
        // }
        // };
        if (Util.hasInternet() && info.isThumbnailDownloading == false)
            new Thread() {
                @Override
                public void run() {
                    try {
                        info.isThumbnailDownloading = true;
                        String u;
                        if (url == null || url.length() == 0) {
                            if (!DownloadUtils.getVideoInfo(info)) {
                                info.isThumbnailDownloading = false;
                                return;
                            }
 
                            u = info.imgUrl;
                        } else {
                            u = url;
                        }
                        Logger.d(TAG, "url:" + u);
                        InputStream i = loadImageFromUrl(u);
                        File file = new File(pathName);
                        if (file.exists()) {
                            file.delete();
                        }
                        FileOutputStream fos = new FileOutputStream(file);
                        byte[] data = new byte[1024];
                        int len1 = 0;
                        while ((len1 = i.read(data)) > 0) {
                            fos.write(data, 0, len1);
                        }
                        fos.flush();
                        fos.close();
                        i.close();
                        Drawable d = Drawable.createFromPath(pathName);
                        imageCache
                                .put(pathName, new SoftReference<Drawable>(d));
                        // Message message = handler.obtainMessage(0, d);
                        // handler.sendMessage(message);
                    } catch (IOException e) {
                        Logger.e(TAG, "loadDrawable", e);
                    } catch (Exception e) {
                        Logger.e(TAG, "loadDrawable", e);
                    } finally {
                        info.isThumbnailDownloading = false;
                    }
                }
            }.start();
        return null;
    }
 
    public Drawable loadDrawable(final String imageUrl,
            final ImageCallback imageCallback) {
        if (imageCache.containsKey(imageUrl)) {
            SoftReference<Drawable> softReference = imageCache.get(imageUrl);
            Drawable drawable = softReference.get();
            if (drawable != null) {
                return drawable;
            }
        }
        final Handler handler = new Handler() {
            public void handleMessage(Message message) {
                imageCallback.imageLoaded((Drawable) message.obj, imageUrl);
            }
        };
        new Thread() {
            @Override
            public void run() {
                Drawable d = Drawable.createFromStream(
                        loadImageFromUrl(imageUrl), "src");
                imageCache.put(imageUrl, new SoftReference<Drawable>(d));
                Message message = handler.obtainMessage(0, d);
                handler.sendMessage(message);
            }
        }.start();
        return null;
    }
 
    private InputStream loadImageFromUrl(String url) {
        try {
            URL u = new URL(url);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            c.setDoInput(true);
            c.connect();
            return (InputStream) c.getInputStream();
        } catch (MalformedURLException e) {
            Logger.e(TAG, "loadImageFromUrl", e);
        } catch (IOException e) {
            Logger.e(TAG, "loadImageFromUrl", e);
        }
        return null;
    }
 
    public interface ImageCallback {
        public void imageLoaded(Drawable imageDrawable, String imageUrl);
    }
}