-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFacebookImageLoader.java
More file actions
executable file
·434 lines (382 loc) · 12.6 KB
/
Copy pathFacebookImageLoader.java
File metadata and controls
executable file
·434 lines (382 loc) · 12.6 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Based on http://code.google.com/p/android-imagedownloader/
* Updated by Harry Tormey <harry@catch.com>
*/
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.io.InputStream;
import java.io.File;
import java.io.FilterInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Build;
import android.util.Log;
import android.util.DisplayMetrics;
import android.widget.ImageView;
import android.view.WindowManager;
public class FacebookImageLoader {
// These are for 1.5 backwards compatibility which doesn't have
// these definied in DisplayMetrics
public static final int DENSITY_LOW = 120;
public static final int DENSITY_MEDIUM = 160;
public static final int DENSITY_HIGH = 240;
private static final String LOGCAT_NAME = "FacebookImageLoader";
private static final int BUFFER_SIZE = 8 * 1024;
private static final String BASE_URL = "http://graph.facebook.com/";
private static final String PICTURE = "/picture";
private static int mDensityDpi = 0;
private Context mContext;
private int mMaxDimension;
public FacebookImageLoader(Context context) {
mContext = context;
mMaxDimension = getMaxThumbnailDimension(mContext, false);
}
public void load(String filename, ImageView imageView) {
Bitmap bitmap = getBitmapFromCache(filename);
if (bitmap == null) {
forceLoad(filename, imageView);
} else {
imageView.setImageBitmap(bitmap);
}
}
private void forceLoad(String filename, ImageView imageView) {
// State sanity: filename is guaranteed to never be null in LoadedDrawable and cache keys.
if (filename == null) {
imageView.setImageDrawable(null);
return;
}
BitmapLoaderTask task = new BitmapLoaderTask(imageView);
//This is where we tie a reference to the image filename to ImageView.
LoadedDrawable downloadedDrawable = new LoadedDrawable(filename);
imageView.setImageDrawable(downloadedDrawable);
task.execute(filename);
}
//Check to see if given filename matches that associated with ImageView. We need this because Listview recycles ImageViews.
private static boolean checkImageViewFileName(ImageView imageView, String filename) {
if (imageView != null) {
Drawable drawable = imageView.getDrawable();
if (drawable instanceof LoadedDrawable) {
LoadedDrawable loadedDrawable = (LoadedDrawable)drawable;
return loadedDrawable.checkFilname(filename);
}
}
return false;
}
Bitmap downloadBitmap(final String url, File cacheFile) {
// HttpClient works with older Android versions.
final HttpClient client = new DefaultHttpClient();
final HttpGet getRequest = new HttpGet(url);
try {
HttpResponse response = client.execute(getRequest);
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w(LOGCAT_NAME, "Error " + statusCode + " while retrieving bitmap from " + url);
return null;
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
inputStream = entity.getContent();
FlushedInputStream in = new FlushedInputStream(inputStream);
if(cacheFile != null && cacheFile.exists()){
FileOutputStream fos = new FileOutputStream(cacheFile);
while(true){
int bytedata = in.read();
if(bytedata == -1)
break;
fos.write(bytedata);
}
fos.close();
return BitmapFactory.decodeFile(cacheFile.getAbsolutePath());
}
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
} catch (IOException e) {
getRequest.abort();
Log.w(LOGCAT_NAME, "I/O error while retrieving bitmap from " + url, e);
} catch (IllegalStateException e) {
getRequest.abort();
Log.w(LOGCAT_NAME, "Incorrect URL: " + url);
} catch (Exception e) {
getRequest.abort();
Log.w(LOGCAT_NAME, "Error while retrieving bitmap from " + url, e);
} finally {
}
return null;
}
private Bitmap loadBitmap(String filename) {
//First check if file exists, if not try and do the facebook fetch
Bitmap bitmap = null;
File cacheFile = FileUtil.getFileFromCache(mContext, filename);
if(cacheFile != null && cacheFile.exists()){
bitmap = loadImageFromFile(cacheFile.getPath(), mMaxDimension, true);
return bitmap;
}else{
//Download from FB and cache
cacheFile = FileUtil.addFileToCache(mContext, filename);
final String url = BASE_URL + filename + PICTURE;
bitmap = downloadBitmap(url, cacheFile);
}
return bitmap;
}
//An InputStream that skips the exact number of bytes provided, unless it reaches EOF.
static class FlushedInputStream extends FilterInputStream {
public FlushedInputStream(InputStream inputStream) {
super(inputStream);
}
@Override
public long skip(long n) throws IOException {
long totalBytesSkipped = 0L;
while (totalBytesSkipped < n) {
long bytesSkipped = in.skip(n - totalBytesSkipped);
if (bytesSkipped == 0L) {
int b = read();
if (b < 0) {
break; // we reached EOF
} else {
bytesSkipped = 1; // we read one byte
}
}
totalBytesSkipped += bytesSkipped;
}
return totalBytesSkipped;
}
}
private class BitmapLoaderTask extends AsyncTask<String, Void, Bitmap> {
private String filename;
private final WeakReference<ImageView> imageViewReference;
public BitmapLoaderTask(ImageView imageView) {
imageViewReference = new WeakReference<ImageView>(imageView);
}
@Override
protected Bitmap doInBackground(String... params) {
filename = params[0];
Bitmap bitmap = loadBitmap(filename);
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap = null;
}
ImageView imageView = imageViewReference.get();
if (imageView != null) {
boolean filenamesMatch = checkImageViewFileName(imageView, filename);
if (imageView != null && filenamesMatch) {
imageView.setImageBitmap(bitmap);
}
}
}
}
static class LoadedDrawable extends ColorDrawable {
private final String mFilename;
public LoadedDrawable(String filename) {
super(Color.TRANSPARENT);
this.mFilename = filename;
}
//Check to see if filename of image downloaded matches filename associated with current ImageView
public boolean checkFilname(String filename){
return mFilename.equals(filename);
}
}
private Bitmap getBitmapFromCache(String filename) {
File cacheFile = FileUtil.getFileFromCache(mContext, filename);
if(cacheFile != null && cacheFile.exists()){
try{
final Bitmap bm = BitmapFactory.decodeStream(new FileInputStream(cacheFile));
return bm;
}catch (Exception e) {
Log.e(LOGCAT_NAME, "Error reading file: " + e.toString());
}
}
return null;
}
public void clearCache() {
FileUtil.cleanCaches(mContext);
}
// Process an an image from a file, resizing it as necessary.
public static Bitmap loadImageFromFile(final String file, final int maxDimension, boolean exactResize) {
// Check input
if (file == null || file.length() == 0) {
return null;
}
BufferedInputStream is = null;
Bitmap image = null;
if (exactResize) {
// caller wants the output bitmap's max dimension scaled exactly
// as passed in. This is slower.
try {
is = new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE);
image = BitmapFactory.decodeStream(is);
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (OutOfMemoryError e) {
// don't return, we'll try again with an inexact resize
image = null;
}
if (image != null) {
return processImageFromBitmap(image, maxDimension);
}
}
// Caller does not need an exact image resize; we can achieve
// "ballpark" using inSampleSize to save time and memory.
BitmapFactory.Options opts = getImageSizeFromFile(file);
if (opts == null) {
return null;
}
// Calculate the resize ratio. Make the longest side
// somewhere in the vicinity of 'maxDimension' pixels.
int scaler = 1;
int maxSide = Math.max(opts.outWidth, opts.outHeight);
if (maxSide > maxDimension) {
float ratio = (float) maxSide / (float) maxDimension;
scaler = Math.round(ratio);
}
opts.inJustDecodeBounds = false;
opts.inSampleSize = scaler;
// This time we'll load the image for real, but with
// inSampleSize set which will scale the image down as it is
// loaded.
try {
is = new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE);
image = BitmapFactory.decodeStream(is, null, opts);
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (OutOfMemoryError e) {
e.printStackTrace();
return null;
}
return image;
}
// Process an an image from a Bitmap already in memory,
// resizing it as necessary.
public static Bitmap processImageFromBitmap(final Bitmap bitmap, final int maxDimension) {
// Check input
if (bitmap == null) {
return null;
}
// Calculate the resize ratio. Make the longest side
// somewhere in the vicinity of 'maxDimension' pixels.
int maxSide = Math.max(bitmap.getWidth(), bitmap.getHeight());
float newWidth = bitmap.getWidth();
float newHeight = bitmap.getHeight();
float ratio = 0;
if (maxSide > maxDimension) {
ratio = (float) maxDimension / (float) maxSide;
newWidth *= ratio;
newHeight *= ratio;
}
try {
return Bitmap.createScaledBitmap(bitmap,
Math.round(newWidth),
Math.round(newHeight), false);
} catch (OutOfMemoryError e) {
e.printStackTrace();
return null;
}
}
public static BitmapFactory.Options getImageSizeFromFile(final String file) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
// With inJustDecodeBounds set, we're just going to peek at
// the resolution of the image.
try {
BufferedInputStream is = new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE);
BitmapFactory.decodeStream(is, null, opts);
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (OutOfMemoryError e) {
e.printStackTrace();
return null;
}
return opts;
}
public static int getMaxThumbnailDimension(Context context, boolean larger) {
if (mDensityDpi == 0) {
calculateDensityDpi(context);
}
switch (mDensityDpi) {
case DENSITY_LOW:
return (larger) ? 64 : 48;
case DENSITY_HIGH:
return (larger) ? 128 : 96;
case DENSITY_MEDIUM:
default:
return (larger) ? 96 : 64;
}
}
public static int calculateDensityDpi(Context context) {
if (mDensityDpi > 0) {
// we've already calculated it
return mDensityDpi;
}
if (Integer.parseInt(Build.VERSION.SDK) <= 3) {
// 1.5 devices are all medium density
mDensityDpi = DENSITY_MEDIUM;
return mDensityDpi;
}
DisplayMetrics metrics = new DisplayMetrics();
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(metrics);
int reflectedDensityDpi = DENSITY_MEDIUM;
try {
reflectedDensityDpi = DisplayMetrics.class.getDeclaredField("mDensityDpi").getInt(metrics);
} catch (Exception e) {
e.printStackTrace();
}
mDensityDpi = reflectedDensityDpi;
return mDensityDpi;
}
}