Search in sources :

Example 11 with BitmapInfo

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

the class BitmapCallback method getBitmapSnapshot.

public static void getBitmapSnapshot(final Ion ion, final String transformKey, final ArrayList<PostProcess> postProcess) {
    // don't do this if this is already loading
    if (ion.bitmapsPending.tag(transformKey) != null)
        return;
    final BitmapCallback callback = new LoadBitmapBase(ion, transformKey, true);
    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(file, null);
                if (bitmap == null)
                    throw new Exception("Bitmap failed to load");
                BitmapInfo info = new BitmapInfo(transformKey, "image/jpeg", bitmap, null);
                info.servedFrom = ResponseServedFrom.LOADED_FROM_CACHE;
                if (postProcess != null) {
                    for (PostProcess p : postProcess) {
                        p.postProcess(info);
                    }
                }
                callback.report(null, info);
            } catch (OutOfMemoryError e) {
                callback.report(new Exception(e), null);
            } catch (Exception e) {
                callback.report(e, null);
                try {
                    ion.responseCache.getFileCache().remove(transformKey);
                } catch (Exception ex) {
                }
            }
        }
    });
}
Also used : Bitmap(android.graphics.Bitmap) PostProcess(com.koushikdutta.ion.bitmap.PostProcess) File(java.io.File) CancellationException(java.util.concurrent.CancellationException) BitmapInfo(com.koushikdutta.ion.bitmap.BitmapInfo)

Example 12 with BitmapInfo

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

the class BitmapCallback method report.

protected void report(final Exception e, final BitmapInfo info) {
    AsyncServer.post(Ion.mainHandler, new Runnable() {

        @Override
        public void run() {
            BitmapInfo result = info;
            if (result == null) {
                // cache errors, unless they were cancellation exceptions
                result = new BitmapInfo(key, null, null, new Point());
                result.exception = e;
                if (!(e instanceof CancellationException))
                    ion.getBitmapCache().put(result);
            } else if (put()) {
                ion.getBitmapCache().put(result);
            } else {
                ion.getBitmapCache().putSoft(result);
            }
            final ArrayList<FutureCallback<BitmapInfo>> callbacks = ion.bitmapsPending.remove(key);
            if (callbacks == null || callbacks.size() == 0) {
                onReported();
                return;
            }
            for (FutureCallback<BitmapInfo> callback : callbacks) {
                callback.onCompleted(e, result);
            }
            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
    !put || // don't cache dead bitmaps
    info.bitmap == null || // don't cache gifs
    info.gifDecoder != null || // too big
    info.sizeOf() > 512 * 512 * 4) {
        return;
    }
    saveBitmapSnapshot(ion, info);
}
Also used : CancellationException(java.util.concurrent.CancellationException) ArrayList(java.util.ArrayList) Point(android.graphics.Point) BitmapInfo(com.koushikdutta.ion.bitmap.BitmapInfo) FutureCallback(com.koushikdutta.async.future.FutureCallback)

Example 13 with BitmapInfo

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

the class StreamLoader method loadGif.

protected BitmapInfo loadGif(String key, Point size, InputStream in, BitmapFactory.Options options) throws Exception {
    GifDecoder gifDecoder = new GifDecoder(ByteBuffer.wrap(StreamUtility.readToEndAsArray(in)));
    GifFrame frame = gifDecoder.nextFrame();
    BitmapInfo info = new BitmapInfo(key, options.outMimeType, frame.image, size);
    info.gifDecoder = gifDecoder;
    return info;
}
Also used : GifDecoder(com.koushikdutta.ion.gif.GifDecoder) GifFrame(com.koushikdutta.ion.gif.GifFrame) BitmapInfo(com.koushikdutta.ion.bitmap.BitmapInfo)

Example 14 with BitmapInfo

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

the class BitmapFetcher method fastLoad.

private boolean fastLoad(String uri) {
    Ion ion = builder.ion;
    if (deepZoom) {
        if (uri == null || !uri.startsWith("file:/"))
            return false;
        File file = new File(URI.create(uri));
        if (!file.exists())
            return false;
        MediaFile.MediaFileType type = MediaFile.getFileType(file.getAbsolutePath());
        if (type == null || !MediaFile.isVideoFileType(type.fileType)) {
            LoadDeepZoom loadDeepZoom = new LoadDeepZoom(ion, decodeKey, animateGif, null);
            loadDeepZoom.onCompleted(null, new Response<File>(null, ResponseServedFrom.LOADED_FROM_CACHE, null, null, file));
            // System.out.println("fastloading deepZoom");
            return true;
        }
    // fall through to allow some other loader to open this, cause this is a video file
    }
    boolean put = !hasTransforms;
    for (Loader loader : ion.configure().getLoaders()) {
        Future<BitmapInfo> future = loader.loadBitmap(builder.contextReference.getContext(), ion, decodeKey, uri, sampleWidth, sampleHeight, animateGif);
        if (future != null) {
            final BitmapCallback callback = new LoadBitmapBase(ion, decodeKey, put);
            future.setCallback(new FutureCallback<BitmapInfo>() {

                @Override
                public void onCompleted(Exception e, BitmapInfo result) {
                    callback.report(e, result);
                }
            });
            // System.out.println("fastloading");
            return true;
        }
    }
    return false;
}
Also used : MediaFile(com.koushikdutta.ion.loader.MediaFile) File(java.io.File) MediaFile(com.koushikdutta.ion.loader.MediaFile) BitmapInfo(com.koushikdutta.ion.bitmap.BitmapInfo)

Example 15 with BitmapInfo

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

the class IonImageViewRequestBuilder method setIonDrawable.

private IonDrawable setIonDrawable(ImageView imageView, BitmapFetcher bitmapFetcher, ResponseServedFrom servedFrom) {
    BitmapInfo info = null;
    if (bitmapFetcher != null)
        info = bitmapFetcher.info;
    if (info != null)
        bitmapFetcher = null;
    IonDrawable ret = IonDrawable.getOrCreateIonDrawable(imageView).ion(ion).setBitmap(info, servedFrom).setBitmapFetcher(bitmapFetcher).setRepeatAnimation(animateGifMode == AnimateGifMode.ANIMATE).setSize(resizeWidth, resizeHeight).setError(errorResource, errorDrawable).setPlaceholder(placeholderResource, placeholderDrawable).setFadeIn(fadeIn || crossfade).setBitmapDrawableFactory(bitmapDrawableFactory).updateLayers();
    imageView.setImageDrawable(ret);
    return ret;
}
Also used : 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