您好我已经编写了以下代码来将图像下载到我的应用程序中。
public class ImageDownloader
{
private static final int DELAY_BEFORE_PURGE = 10000;
private static final int HARD_CACHE_CAPACITY = 10;
private static final String LOG_TAG = "ImageDownloader";
@SuppressWarnings("unchecked")
private static final ConcurrentHashMap<String, SoftReference<Bitmap>> sSoftBitmapCache = new ConcurrentHashMap<String, SoftReference<Bitmap>>(5);
private Mode1 mode = Mode1.CORRECT;
private final Handler purgeHandler = new Handler();
private final Runnable purger = new Runnable()
{
public void run()
{
ImageDownloader.this.clearCache();
}
};
private final HashMap<String, Bitmap> sHardBitmapCache = new LinkedHashMap(5, 0.75F, true)
{
protected boolean removeEldestEntry(Map.Entry<String, Bitmap> paramAnonymousEntry)
{
if (size() > 10)
ImageDownloader.sSoftBitmapCache.put((String)paramAnonymousEntry.getKey(), new SoftReference((Bitmap)paramAnonymousEntry.getValue()));
for (boolean bool = true; ; bool = false)
return bool;
}
};
private Bitmap localObject2; private void addBitmapToCache(String paramString, Bitmap paramBitmap)
{
if (paramBitmap != null)
synchronized (this.sHardBitmapCache)
{
this.sHardBitmapCache.put(paramString, paramBitmap);
}
}
private static boolean cancelPotentialDownload(String paramString, ImageView paramImageView)
{
boolean bool = true;
BitmapDownloaderTask localBitmapDownloaderTask = getBitmapDownloaderTask(paramImageView);
if (localBitmapDownloaderTask != null)
{
String str = localBitmapDownloaderTask.url;
if ((str != null) && (str.equals(paramString)))
break label39;
localBitmapDownloaderTask.cancel(bool);
}
while (true)
{
return bool;
label39: bool = false;
}
}
private void forceDownload(String paramString, ImageView paramImageView)
{
if (paramString == null)
paramImageView.setImageDrawable(null);
while (true)
{
return;
if (cancelPotentialDownload(paramString, paramImageView))
switch ($SWITCH_TABLE$org$fluturasymphony$recommendation$ImageDownloader$Mode()[this.mode.ordinal()])
{
default:
break;
case 1:
Bitmap localBitmap = downloadBitmap(paramString);
addBitmapToCache(paramString, localBitmap);
paramImageView.setImageBitmap(localBitmap);
break;
case 2:
paramImageView.setMinimumHeight(156);
new BitmapDownloaderTask(paramImageView).execute(new String[] { paramString });
break;
case 3:
BitmapDownloaderTask localBitmapDownloaderTask = new BitmapDownloaderTask(paramImageView);
paramImageView.setImageDrawable(new DownloadedDrawable(localBitmapDownloaderTask));
paramImageView.setMinimumHeight(156);
localBitmapDownloaderTask.execute(new String[] { paramString });
}
}
}
private int[] $SWITCH_TABLE$org$fluturasymphony$recommendation$ImageDownloader$Mode() {
// TODO Auto-generated method stub
return null;
}
private static BitmapDownloaderTask getBitmapDownloaderTask(ImageView paramImageView)
{
Drawable localDrawable;
if (paramImageView != null)
{
localDrawable = paramImageView.getDrawable();
if (!(localDrawable instanceof DownloadedDrawable));
}
for (BitmapDownloaderTask localBitmapDownloaderTask = ((DownloadedDrawable)localDrawable).getBitmapDownloaderTask(); ; localBitmapDownloaderTask = null)
return localBitmapDownloaderTask;
}
private Bitmap getBitmapFromCache(String paramString)
{
synchronized (this.sHardBitmapCache)
{
Bitmap localBitmap1 = (Bitmap)this.sHardBitmapCache.get(paramString);
if (localBitmap1 != null)
{
this.sHardBitmapCache.remove(paramString);
this.sHardBitmapCache.put(paramString, localBitmap1);
localObject2 = localBitmap1;
}
else
{
SoftReference localSoftReference = (SoftReference)sSoftBitmapCache.get(paramString);
if (localSoftReference != null)
{
Bitmap localBitmap2 = (Bitmap)localSoftReference.get();
if (localBitmap2 != null)
localObject2 = localBitmap2;
}
}
}
sSoftBitmapCache.remove(paramString);
Object localObject2 = null;
return (Bitmap) localObject2;
}
private void resetPurgeTimer()
{
this.purgeHandler.removeCallbacks(this.purger);
this.purgeHandler.postDelayed(this.purger, 10000L);
}
public void clearCache()
{
this.sHardBitmapCache.clear();
sSoftBitmapCache.clear();
}
public void download(String paramString, ImageView paramImageView)
{
resetPurgeTimer();
Bitmap localBitmap = getBitmapFromCache(paramString);
if (localBitmap == null)
forceDownload(paramString, paramImageView);
while (true)
{
return;
cancelPotentialDownload(paramString, paramImageView);
paramImageView.setImageBitmap(localBitmap);
}
}
// ERROR //
Bitmap downloadBitmap(String paramString)
{
// Byte code:
// 0: aload_0
// 1: getfield 65 org/fluturasymphony/recommendation/ImageDownloader:mode Lorg/fluturasymphony/recommendation/ImageDownloader$Mode;
// 4: getstatic 48 org/fluturasymphony/recommendation/ImageDownloader$Mode:NO_ASYNC_TASK Lorg/fluturasymphony/recommendation/ImageDownloader$Mode;
// 7: if_acmpne +107 -> 114
// 10: new 213 org/apache/http/impl/client/DefaultHttpClient
// 13: dup
// 14: invokespecial 214 org/apache/http/impl/client/DefaultHttpClient:<init> ()V
// 17: astore_2
// 18: new 216 org/apache/http/client/methods/HttpGet
// 21: dup
// 22: aload_1
// 23: invokespecial 219 org/apache/http/client/methods/HttpGet:<init> (Ljava/lang/String;)V
// 26: astore_3
// 27: aload_2
// 28: aload_3
// 29: invokeinterface 224 2 0
// 34: astore 15
// 36: aload 15
// 38: invokeinterface 230 1 0
// 43: invokeinterface 235 1 0
// 48: istore 16
// 50: iload 16
// 52: sipush 200
// 55: if_icmpeq +69 -> 124
// 58: ldc 15
// 60: new 237 java/lang/StringBuilder
// 63: dup
// 64: ldc 239
// 66: invokespecial 240 java/lang/StringBuilder:<init> (Ljava/lang/String;)V
// 69: iload 16
// 71: invokevirtual 244 java/lang/StringBuilder:append (I)Ljava/lang/StringBuilder;
// 74: ldc 246
// 76: invokevirtual 249 java/lang/StringBuilder:append (Ljava/lang/String;)Ljava/lang/StringBuilder;
// 79: aload_1
// 80: invokevirtual 249 java/lang/StringBuilder:append (Ljava/lang/String;)Ljava/lang/StringBuilder;
// 83: invokevirtual 253 java/lang/StringBuilder:toString ()Ljava/lang/String;
// 86: invokestatic 259 android/util/Log:w (Ljava/lang/String;Ljava/lang/String;)I
// 89: pop
// 90: aload_2
// 91: instanceof 261
// 94: istore 23
// 96: aconst_null
// 97: astore 8
// 99: iload 23
// 101: ifeq +10 -> 111
// 104: aload_2
// 105: checkcast 261 android/net/http/AndroidHttpClient
// 108: invokevirtual 264 android/net/http/AndroidHttpClient:close ()V
// 111: aload 8
// 113: areturn
// 114: ldc_w 266
// 117: invokestatic 270 android/net/http/AndroidHttpClient:newInstance (Ljava/lang/String;)Landroid/net/http/AndroidHttpClient;
// 120: astore_2
// 121: goto -103 -> 18
// 124: aload 15
// 126: invokeinterface 274 1 0
// 131: astore 17
// 133: aload 17
// 135: ifnull +280 -> 415
// 138: aconst_null
// 139: astore 19
// 141: aload 17
// 143: invokeinterface 280 1 0
// 148: astore 19
// 150: new 282 org/fluturasymphony/recommendation/ImageDownloader$FlushedInputStream
// 153: dup
// 154: aload 19
// 156: invokespecial 285 org/fluturasymphony/recommendation/ImageDownloader$FlushedInputStream:<init> (Ljava/io/InputStream;)V
// 159: invokestatic 291 android/graphics/BitmapFactory:decodeStream (Ljava/io/InputStream;)Landroid/graphics/Bitmap;
// 162: astore 21
// 164: aload 19
// 166: ifnull +8 -> 174
// 169: aload 19
// 171: invokevirtual 294 java/io/InputStream:close ()V
// 174: aload 17
// 176: invokeinterface 297 1 0
// 181: aload_2
// 182: instanceof 261
// 185: ifeq +10 -> 195
// 188: aload_2
// 189: checkcast 261 android/net/http/AndroidHttpClient
// 192: invokevirtual 264 android/net/http/AndroidHttpClient:close ()V
// 195: aload 21
// 197: astore 8
// 199: goto -88 -> 111
// 202: astore 20
// 204: aload 19
// 206: ifnull +8 -> 214
// 209: aload 19
// 211: invokevirtual 294 java/io/InputStream:close ()V
// 214: aload 17
// 216: invokeinterface 297 1 0
// 221: aload 20
// 223: athrow
// 224: astore 12
// 226: aload_3
// 227: invokevirtual 300 org/apache/http/client/methods/HttpGet:abort ()V
// 230: ldc 15
// 232: new 237 java/lang/StringBuilder
// 235: dup
// 236: ldc_w 302
// 239: invokespecial 240 java/lang/StringBuilder:<init> (Ljava/lang/String;)V
// 242: aload_1
// 243: invokevirtual 249 java/lang/StringBuilder:append (Ljava/lang/String;)Ljava/lang/StringBuilder;
// 246: invokevirtual 253 java/lang/StringBuilder:toString ()Ljava/lang/String;
// 249: aload 12
// 251: invokestatic 305 android/util/Log:w (Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;)I
// 254: pop
// 255: aload_2
// 256: instanceof 261
// 259: istore 14
// 261: aconst_null
// 262: astore 8
// 264: iload 14
// 266: ifeq -155 -> 111
// 269: aload_2
// 270: checkcast 261 android/net/http/AndroidHttpClient
// 273: invokevirtual 264 android/net/http/AndroidHttpClient:close ()V
// 276: aconst_null
// 277: astore 8
// 279: goto -168 -> 111
// 282: astore 9
// 284: aload_3
// 285: invokevirtual 300 org/apache/http/client/methods/HttpGet:abort ()V
// 288: ldc 15
// 290: new 237 java/lang/StringBuilder
// 293: dup
// 294: ldc_w 307
// 297: invokespecial 240 java/lang/StringBuilder:<init> (Ljava/lang/String;)V
// 300: aload_1
// 301: invokevirtual 249 java/lang/StringBuilder:append (Ljava/lang/String;)Ljava/lang/StringBuilder;
// 304: invokevirtual 253 java/lang/StringBuilder:toString ()Ljava/lang/String;
// 307: invokestatic 259 android/util/Log:w (Ljava/lang/String;Ljava/lang/String;)I
// 310: pop
// 311: aload_2
// 312: instanceof 261
// 315: istore 11
// 317: aconst_null
// 318: astore 8
// 320: iload 11
// 322: ifeq -211 -> 111
// 325: aload_2
// 326: checkcast 261 android/net/http/AndroidHttpClient
// 329: invokevirtual 264 android/net/http/AndroidHttpClient:close ()V
// 332: aconst_null
// 333: astore 8
// 335: goto -224 -> 111
// 338: astore 5
// 340: aload_3
// 341: invokevirtual 300 org/apache/http/client/methods/HttpGet:abort ()V
// 344: ldc 15
// 346: new 237 java/lang/StringBuilder
// 349: dup
// 350: ldc_w 309
// 353: invokespecial 240 java/lang/StringBuilder:<init> (Ljava/lang/String;)V
// 356: aload_1
// 357: invokevirtual 249 java/lang/StringBuilder:append (Ljava/lang/String;)Ljava/lang/StringBuilder;
// 360: invokevirtual 253 java/lang/StringBuilder:toString ()Ljava/lang/String;
// 363: aload 5
// 365: invokestatic 305 android/util/Log:w (Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;)I
// 368: pop
// 369: aload_2
// 370: instanceof 261
// 373: istore 7
// 375: aconst_null
// 376: astore 8
// 378: iload 7
// 380: ifeq -269 -> 111
// 383: aload_2
// 384: checkcast 261 android/net/http/AndroidHttpClient
// 387: invokevirtual 264 android/net/http/AndroidHttpClient:close ()V
// 390: aconst_null
// 391: astore 8
// 393: goto -282 -> 111
// 396: astore 4
// 398: aload_2
// 399: instanceof 261
// 402: ifeq +10 -> 412
// 405: aload_2
// 406: checkcast 261 android/net/http/AndroidHttpClient
// 409: invokevirtual 264 android/net/http/AndroidHttpClient:close ()V
// 412: aload 4
// 414: athrow
// 415: aload_2
// 416: instanceof 261
// 419: istore 18
// 421: aconst_null
// 422: astore 8
// 424: iload 18
// 426: ifeq -315 -> 111
// 429: aload_2
// 430: checkcast 261 android/net/http/AndroidHttpClient
// 433: invokevirtual 264 android/net/http/AndroidHttpClient:close ()V
// 436: aconst_null
// 437: astore 8
// 439: goto -328 -> 111
//
// Exception table:
// from to target type
// 141 164 202 finally
// 27 90 224 java/io/IOException
// 124 133 224 java/io/IOException
// 169 181 224 java/io/IOException
// 209 224 224 java/io/IOException
// 27 90 282 java/lang/IllegalStateException
// 124 133 282 java/lang/IllegalStateException
// 169 181 282 java/lang/IllegalStateException
// 209 224 282 java/lang/IllegalStateException
// 27 90 338 java/lang/Exception
// 124 133 338 java/lang/Exception
// 169 181 338 java/lang/Exception
// 209 224 338 java/lang/Exception
// 27 90 396 finally
// 124 133 396 finally
// 169 181 396 finally
// 209 224 396 finally
// 226 255 396 finally
// 284 311 396 finally
// 340 369 396 finally
}
public void setMode(Mode1 paramMode)
{
this.mode = paramMode;
clearCache();
}
class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap>
{
private final WeakReference<ImageView> imageViewReference;
private String url;
public BitmapDownloaderTask(ImageView arg2)
{
Object localObject;
this.imageViewReference = new WeakReference(localObject);
}
protected Bitmap doInBackground(String[] paramArrayOfString)
{
this.url = paramArrayOfString[0];
return ImageDownloader.this.downloadBitmap(this.url);
}
protected void onPostExecute(Bitmap paramBitmap)
{
if (isCancelled())
paramBitmap = null;
ImageDownloader.this.addBitmapToCache(this.url, paramBitmap);
if (this.imageViewReference != null)
{
ImageView localImageView = (ImageView)this.imageViewReference.get();
if ((this == ImageDownloader.getBitmapDownloaderTask(localImageView)) || (ImageDownloader.this.mode != ImageDownloader.Mode1.CORRECT))
localImageView.setImageBitmap(paramBitmap);
}
}
}
static class DownloadedDrawable extends ColorDrawable
{
private final WeakReference<ImageDownloader.BitmapDownloaderTask> bitmapDownloaderTaskReference;
public DownloadedDrawable(ImageDownloader.BitmapDownloaderTask paramBitmapDownloaderTask)
{
super();
this.bitmapDownloaderTaskReference = new WeakReference<BitmapDownloaderTask>(paramBitmapDownloaderTask);
}
public ImageDownloader.BitmapDownloaderTask getBitmapDownloaderTask()
{
return (ImageDownloader.BitmapDownloaderTask)this.bitmapDownloaderTaskReference.get();
}
}
static class FlushedInputStream extends FilterInputStream
{
public FlushedInputStream(InputStream paramInputStream)
{
super(paramInputStream);
}
public long skip(long paramLong)
throws IOException
{
long l2;
for (long l1 = 0L; ; l1 += l2)
{
if (l1 >= paramLong);
do
{
return l1;
l2 = this.in.skip(paramLong - l1);
if (l2 != 0L)
break;
}
while (read() < 0);
l2 = 1L;
}
}
}
public static enum Mode1{
CORRECT ( new Mode1("CORRECT", 2),
Mode1[] arrayOfMode = new Mode1[3],
arrayOfMode[0] = NO_ASYNC_TASK,
arrayOfMode[1] = NO_DOWNLOADED_DRAWABLE,
arrayOfMode[2] = CORRECT1);
}
}
上面代码中的错误出现在枚举声明中,可以是我做错的任何地方。
我在这里收到一个错位的构造错误 Mode1 [] ...