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