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) {
}
}
}
});
}
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);
}
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;
}
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;
}
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;
}
Aggregations