Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.koushikdutta.ion;
  
  import  android.graphics.Bitmap;
  import  android.graphics.Point;
  
 
 import java.io.File;
 
 abstract class BitmapCallback {
     String key;
     Ion ion;
 
     public static void saveBitmapSnapshot(Ion ionBitmapInfo info) {
         if (info.bitmap == null)
             return;
         FileCache cache = ion.responseCache.getFileCache();
         if (cache == null)
             return;
         File tempFile = cache.getTempFile();
         try {
             FileOutputStream out = new FileOutputStream(tempFile);
             Bitmap.CompressFormat format = info.bitmap.hasAlpha() ? Bitmap.CompressFormat.PNG : Bitmap.CompressFormat.JPEG;
             info.bitmap.compress(format, 100, out);
             out.close();
             cache.commitTempFiles(info.keytempFile);
         }
         catch (Exception ex) {
         }
         finally {
             tempFile.delete();
         }
     }
 
     public static void getBitmapSnapshot(final Ion ionfinal String transformKeyfinal ArrayList<PostProcesspostProcess) {
         // don't do this if this is already loading
         if (ion.bitmapsPending.tag(transformKey) != null)
             return;
         final BitmapCallback callback = new LoadBitmapBase(iontransformKeytrue);
         Ion.getBitmapLoadExecutorService().execute(new Runnable() {
             @Override
             public void run() {
                 if (ion.bitmapsPending.tag(transformKey) != callback) {
 //                    Log.d("IonBitmapLoader", "Bitmap cache load cancelled (no longer needed)");
                     return;
                 }
 
                 try {
                     File file = ion.responseCache.getFileCache().getFile(transformKey);
                     Bitmap bitmap = IonBitmapCache.loadBitmap(filenull);
                     if (bitmap == null)
                         throw new Exception("Bitmap failed to load");
                     BitmapInfo info = new BitmapInfo(transformKey"image/jpeg"bitmapnull);
                     info.loadedFrom =  ..;
 
                     if (postProcess != null) {
                         for (PostProcess ppostProcess) {
                             p.postProcess(info);
                         }
                     }
 
                     callback.report(nullinfo);
                 }
                 catch (OutOfMemoryError e) {
                     callback.report(new Exception(e), null);
                 }
                 catch (Exception e) {
                     callback.report(enull);
                     try {
                         ion.responseCache.getFileCache().remove(transformKey);
                     } catch (Exception ex) {
                     }
                 }
             }
         });
     }
 
     protected BitmapCallback(Ion ionString keyboolean put) {
         this. = key;
         this. = put;
         this. = ion;
 
         ion.bitmapsPending.tag(keythis);
     }
 
     boolean put;
 
     boolean put() {
         return ;
     }
 
    protected void onReported() {
        .processDeferred();
    }
    protected void report(final Exception efinal BitmapInfo info) {
        AsyncServer.post(.new Runnable() {
            @Override
            public void run() {
                BitmapInfo result = info;
                if (result == null) {
                    // cache errors, unless they were cancellation exceptions
                    result = new BitmapInfo(nullnullnew Point());
                    result.exception = e;
                    if (!(e instanceof CancellationException))
                        .getBitmapCache().put(result);
                } else if (put()) {
                    .getBitmapCache().put(result);
                }
                else {
                    .getBitmapCache().putSoft(result);
                }
                final ArrayList<FutureCallback<BitmapInfo>> callbacks = ..remove();
                if (callbacks == null || callbacks.size() == 0) {
                    onReported();
                    return;
                }
                for (FutureCallback<BitmapInfocallback : callbacks) {
                    callback.onCompleted(eresult);
                }
                onReported();
            }
        });
        // attempt to smart cache stuff to disk
        if (info == null || info.originalSize == null || info.decoder != null
            // don't cache anything that requests not to be cached
            || !
            // don't cache dead bitmaps
            || info.bitmap == null
            // don't cache gifs
            || info.gifDecoder != null
            // too big
            || info.sizeOf() > 512 * 512 * 4) {
            return;
        }
        saveBitmapSnapshot(info);
    }
New to GrepCode? Check out our FAQ X