Search in sources :

Example 16 with BitmapInfo

use of com.koushikdutta.ion.bitmap.BitmapInfo in project ion by koush.

the class IonBitmapRequestBuilder method isLocallyCached.

@Override
public LocallyCachedStatus isLocallyCached() {
    if (builder.noCache || deepZoom)
        return LocallyCachedStatus.NOT_CACHED;
    final String decodeKey = computeDecodeKey();
    addDefaultTransform();
    String bitmapKey = computeBitmapKey(decodeKey);
    BitmapInfo info = builder.ion.bitmapCache.get(bitmapKey);
    // memory cache
    if (info != null && info.exception == null)
        return LocallyCachedStatus.CACHED;
    FileCache fileCache = ion.responseCache.getFileCache();
    if (hasTransforms() && fileCache.exists(bitmapKey))
        return LocallyCachedStatus.CACHED;
    if (fileCache.exists(decodeKey))
        return LocallyCachedStatus.MAYBE_CACHED;
    return LocallyCachedStatus.NOT_CACHED;
}
Also used : BitmapInfo(com.koushikdutta.ion.bitmap.BitmapInfo) FileCache(com.koushikdutta.async.util.FileCache)

Example 17 with BitmapInfo

use of com.koushikdutta.ion.bitmap.BitmapInfo in project ion by koush.

the class IonBitmapRequestBuilder method executeCache.

BitmapFetcher executeCache(int sampleWidth, int sampleHeight) {
    final String decodeKey = computeDecodeKey();
    String bitmapKey = computeBitmapKey(decodeKey);
    // TODO: eliminate this allocation?
    BitmapFetcher ret = new BitmapFetcher();
    ret.bitmapKey = bitmapKey;
    ret.decodeKey = decodeKey;
    ret.hasTransforms = hasTransforms();
    ret.sampleWidth = sampleWidth;
    ret.sampleHeight = sampleHeight;
    ret.builder = builder;
    ret.transforms = transforms;
    ret.animateGif = animateGifMode != AnimateGifMode.NO_ANIMATE;
    ret.deepZoom = deepZoom;
    ret.postProcess = postProcess;
    // see if this request can be fulfilled from the cache
    if (!builder.noCache) {
        BitmapInfo bitmap = builder.ion.bitmapCache.get(bitmapKey);
        if (bitmap != null) {
            ret.info = bitmap;
            return ret;
        }
    }
    return ret;
}
Also used : BitmapInfo(com.koushikdutta.ion.bitmap.BitmapInfo)

Example 18 with BitmapInfo

use of com.koushikdutta.ion.bitmap.BitmapInfo in project ion by koush.

the class LoadBitmap method onCompleted.

@Override
public void onCompleted(Exception e, final Response<ByteBufferList> response) {
    if (e == null)
        e = response.getException();
    if (e != null) {
        report(e, null);
        return;
    }
    final ByteBufferList result = response.getResult();
    if (ion.bitmapsPending.tag(key) != this) {
        result.recycle();
        return;
    }
    Ion.getBitmapLoadExecutorService().execute(new Runnable() {

        @Override
        public void run() {
            if (ion.bitmapsPending.tag(key) != LoadBitmap.this) {
                result.recycle();
                return;
            }
            ByteBuffer bb = null;
            try {
                bb = result.getAll();
                Bitmap bitmap;
                GifDecoder gifDecoder;
                BitmapFactory.Options options = ion.bitmapCache.prepareBitmapOptions(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining(), resizeWidth, resizeHeight);
                final Point size = new Point(options.outWidth, options.outHeight);
                if (animateGif && TextUtils.equals("image/gif", options.outMimeType)) {
                    // new GifDecoder(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining());
                    gifDecoder = new GifDecoder(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining());
                    GifFrame frame = gifDecoder.nextFrame();
                    bitmap = frame.image;
                    // the byte buffer is needed by the decoder
                    bb = null;
                } else {
                    bitmap = IonBitmapCache.loadBitmap(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining(), options);
                    gifDecoder = null;
                    if (bitmap == null)
                        throw new Exception("failed to load bitmap");
                }
                BitmapInfo info = new BitmapInfo(key, options.outMimeType, bitmap, size);
                info.gifDecoder = gifDecoder;
                info.servedFrom = response.getServedFrom();
                report(null, info);
            } catch (OutOfMemoryError e) {
                report(new Exception(e), null);
            } catch (Exception e) {
                report(e, null);
            } finally {
                ByteBufferList.reclaim(bb);
            }
        }
    });
}
Also used : GifDecoder(com.koushikdutta.ion.gif.GifDecoder) Bitmap(android.graphics.Bitmap) ByteBufferList(com.koushikdutta.async.ByteBufferList) GifFrame(com.koushikdutta.ion.gif.GifFrame) Point(android.graphics.Point) ByteBuffer(java.nio.ByteBuffer) BitmapInfo(com.koushikdutta.ion.bitmap.BitmapInfo)

Example 19 with BitmapInfo

use of com.koushikdutta.ion.bitmap.BitmapInfo in project ion by koush.

the class LoadDeepZoom method onCompleted.

@Override
public void onCompleted(Exception e, final Response<File> response) {
    if (e == null)
        e = response.getException();
    if (e != null) {
        report(e, null);
        return;
    }
    final File tempFile = response.getResult();
    if (ion.bitmapsPending.tag(key) != this) {
        // Log.d("IonBitmapLoader", "Bitmap load cancelled (no longer needed)");
        return;
    }
    Ion.getBitmapLoadExecutorService().execute(new Runnable() {

        @Override
        public void run() {
            FileInputStream fin = null;
            try {
                File file;
                // file cache will be null if the file is on the local file system already
                if (fileCache != null) {
                    fileCache.commitTempFiles(key, tempFile);
                    file = fileCache.getFile(key);
                } else {
                    // local file system, use the "temp" file as the source.
                    file = tempFile;
                }
                BitmapFactory.Options options = ion.getBitmapCache().prepareBitmapOptions(file, 0, 0);
                final Point size = new Point(options.outWidth, options.outHeight);
                if (animateGif && TextUtils.equals("image/gif", options.outMimeType)) {
                    fin = fileCache.get(key);
                    GifDecoder gifDecoder = new GifDecoder(ByteBuffer.wrap(StreamUtility.readToEndAsArray(fin)));
                    GifFrame frame = gifDecoder.nextFrame();
                    BitmapInfo info = new BitmapInfo(key, options.outMimeType, frame.image, size);
                    info.gifDecoder = gifDecoder;
                    report(null, info);
                    return;
                }
                BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(file.toString(), false);
                Bitmap bitmap = decoder.decodeRegion(new Rect(0, 0, size.x, size.y), options);
                if (bitmap == null)
                    throw new Exception("unable to load decoder");
                BitmapInfo info = new BitmapInfo(key, options.outMimeType, bitmap, size);
                info.decoder = decoder;
                info.decoderFile = file;
                info.servedFrom = response.getServedFrom();
                report(null, info);
            } catch (Exception e) {
                report(e, null);
            } finally {
                StreamUtility.closeQuietly(fin);
            }
        }
    });
}
Also used : Rect(android.graphics.Rect) BitmapRegionDecoder(android.graphics.BitmapRegionDecoder) Point(android.graphics.Point) FileInputStream(java.io.FileInputStream) GifDecoder(com.koushikdutta.ion.gif.GifDecoder) Bitmap(android.graphics.Bitmap) GifFrame(com.koushikdutta.ion.gif.GifFrame) File(java.io.File) BitmapInfo(com.koushikdutta.ion.bitmap.BitmapInfo)

Aggregations

BitmapInfo (com.koushikdutta.ion.bitmap.BitmapInfo)19 Bitmap (android.graphics.Bitmap)8 Point (android.graphics.Point)8 SimpleFuture (com.koushikdutta.async.future.SimpleFuture)5 File (java.io.File)5 GifFrame (com.koushikdutta.ion.gif.GifFrame)4 ImageView (android.widget.ImageView)3 GifDecoder (com.koushikdutta.ion.gif.GifDecoder)3 Paint (android.graphics.Paint)2 Rect (android.graphics.Rect)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 CancellationException (java.util.concurrent.CancellationException)2 Intent (android.content.Intent)1 PackageManager (android.content.pm.PackageManager)1 BitmapRegionDecoder (android.graphics.BitmapRegionDecoder)1 Transition (android.transition.Transition)1 View (android.view.View)1 ByteBufferList (com.koushikdutta.async.ByteBufferList)1 FutureCallback (com.koushikdutta.async.future.FutureCallback)1