use of com.facebook.common.references.CloseableReference in project BigImageViewer by Piasy.
the class FrescoImageLoader method loadImage.
@Override
public void loadImage(Uri uri, final Callback callback) {
ImageRequest request = ImageRequest.fromUri(uri);
File localCache = getCacheFile(request);
if (localCache.exists()) {
callback.onCacheHit(localCache);
} else {
// ensure `onStart` is called before `onProgress` and `onFinish`
callback.onStart();
// show 0 progress immediately
callback.onProgress(0);
ImagePipeline pipeline = Fresco.getImagePipeline();
DataSource<CloseableReference<PooledByteBuffer>> source = pipeline.fetchEncodedImage(request, true);
source.subscribe(new ImageDownloadSubscriber(mAppContext) {
@Override
protected void onProgress(int progress) {
callback.onProgress(progress);
}
@Override
protected void onSuccess(File image) {
callback.onFinish();
callback.onCacheMiss(image);
}
@Override
protected void onFail(Throwable t) {
// TODO: 12/11/2016 fail
t.printStackTrace();
}
}, mExecutorSupplier.forBackgroundTasks());
}
}
use of com.facebook.common.references.CloseableReference in project fresco by facebook.
the class CountingMemoryCacheTest method testTrimming.
@Test
public void testTrimming() {
MemoryTrimType memoryTrimType = MemoryTrimType.OnCloseToDalvikHeapLimit;
mParams = new MemoryCacheParams(1100, 10, 1100, 10, 110);
when(mParamsSupplier.get()).thenReturn(mParams);
PowerMockito.when(SystemClock.uptimeMillis()).thenReturn(CountingMemoryCache.PARAMS_INTERCHECK_INTERVAL_MS);
InOrder inOrder = inOrder(mReleaser);
// create original references
CloseableReference<Integer>[] originalRefs = new CloseableReference[10];
for (int i = 0; i < 10; i++) {
originalRefs[i] = newReference(100 + i);
}
// cache items & close the original references
CloseableReference<Integer>[] cachedRefs = new CloseableReference[10];
for (int i = 0; i < 10; i++) {
cachedRefs[i] = mCache.cache(KEYS[i], originalRefs[i]);
originalRefs[i].close();
}
// cache should keep alive the items until evicted
inOrder.verify(mReleaser, never()).release(anyInt());
// trimming cannot evict shared entries
when(mCacheTrimStrategy.getTrimRatio(memoryTrimType)).thenReturn(1.00);
mCache.trim(memoryTrimType);
assertSharedWithCount(KEYS[0], 100, 1);
assertSharedWithCount(KEYS[1], 101, 1);
assertSharedWithCount(KEYS[2], 102, 1);
assertSharedWithCount(KEYS[3], 103, 1);
assertSharedWithCount(KEYS[4], 104, 1);
assertSharedWithCount(KEYS[5], 105, 1);
assertSharedWithCount(KEYS[6], 106, 1);
assertSharedWithCount(KEYS[7], 107, 1);
assertSharedWithCount(KEYS[8], 108, 1);
assertSharedWithCount(KEYS[9], 109, 1);
assertTotalSize(10, 1045);
assertExclusivelyOwnedSize(0, 0);
// close 7 client references
cachedRefs[8].close();
cachedRefs[2].close();
cachedRefs[7].close();
cachedRefs[3].close();
cachedRefs[6].close();
cachedRefs[4].close();
cachedRefs[5].close();
assertSharedWithCount(KEYS[0], 100, 1);
assertSharedWithCount(KEYS[1], 101, 1);
assertSharedWithCount(KEYS[9], 109, 1);
assertExclusivelyOwned(KEYS[8], 108);
assertExclusivelyOwned(KEYS[2], 102);
assertExclusivelyOwned(KEYS[7], 107);
assertExclusivelyOwned(KEYS[3], 103);
assertExclusivelyOwned(KEYS[6], 106);
assertExclusivelyOwned(KEYS[4], 104);
assertExclusivelyOwned(KEYS[5], 105);
assertTotalSize(10, 1045);
assertExclusivelyOwnedSize(7, 735);
// Trim cache by 45%. This means that out of total of 1045 bytes cached, 574 should remain.
// 310 bytes is used by the clients, which leaves 264 for the exclusively owned items.
// Only the two most recent exclusively owned items fit, and they occupy 209 bytes.
when(mCacheTrimStrategy.getTrimRatio(memoryTrimType)).thenReturn(0.45);
mCache.trim(memoryTrimType);
assertSharedWithCount(KEYS[0], 100, 1);
assertSharedWithCount(KEYS[1], 101, 1);
assertSharedWithCount(KEYS[9], 109, 1);
assertExclusivelyOwned(KEYS[4], 104);
assertExclusivelyOwned(KEYS[5], 105);
assertNotCached(KEYS[8], 108);
assertNotCached(KEYS[2], 102);
assertNotCached(KEYS[7], 107);
assertNotCached(KEYS[3], 103);
assertNotCached(KEYS[6], 106);
assertTotalSize(5, 519);
assertExclusivelyOwnedSize(2, 209);
inOrder.verify(mReleaser).release(108);
inOrder.verify(mReleaser).release(102);
inOrder.verify(mReleaser).release(107);
inOrder.verify(mReleaser).release(103);
inOrder.verify(mReleaser).release(106);
// Full trim. All exclusively owned items should be evicted.
when(mCacheTrimStrategy.getTrimRatio(memoryTrimType)).thenReturn(1.00);
mCache.trim(memoryTrimType);
assertSharedWithCount(KEYS[0], 100, 1);
assertSharedWithCount(KEYS[1], 101, 1);
assertSharedWithCount(KEYS[9], 109, 1);
assertNotCached(KEYS[8], 108);
assertNotCached(KEYS[2], 102);
assertNotCached(KEYS[7], 107);
assertNotCached(KEYS[3], 103);
assertNotCached(KEYS[6], 106);
assertNotCached(KEYS[6], 104);
assertNotCached(KEYS[6], 105);
assertTotalSize(3, 310);
assertExclusivelyOwnedSize(0, 0);
inOrder.verify(mReleaser).release(104);
inOrder.verify(mReleaser).release(105);
}
use of com.facebook.common.references.CloseableReference in project DevRing by LJYcoder.
the class FrescoManager method downLoadImage.
@Override
public void downLoadImage(Context context, String url, final File saveFile, final ImageListener<File> imageListener) {
// 参考自https://github.com/hpdx/fresco-helper/blob/master/fresco-helper/src/main/java/com/facebook/fresco/helper/ImageLoader.java
Uri uri = Uri.parse(url);
ImagePipeline imagePipeline = Fresco.getImagePipeline();
ImageRequestBuilder builder = ImageRequestBuilder.newBuilderWithSource(uri);
ImageRequest imageRequest = builder.build();
// 获取未解码的图片数据
DataSource<CloseableReference<PooledByteBuffer>> dataSource = imagePipeline.fetchEncodedImage(imageRequest, context);
dataSource.subscribe(new BaseDataSubscriber<CloseableReference<PooledByteBuffer>>() {
@Override
public void onNewResultImpl(DataSource<CloseableReference<PooledByteBuffer>> dataSource) {
if (!dataSource.isFinished()) {
return;
}
CloseableReference<PooledByteBuffer> imageReference = dataSource.getResult();
if (imageReference != null) {
final CloseableReference<PooledByteBuffer> closeableReference = imageReference.clone();
try {
PooledByteBuffer pooledByteBuffer = closeableReference.get();
InputStream inputStream = new PooledByteBufferInputStream(pooledByteBuffer);
OutputStream outputStream = new FileOutputStream(saveFile);
if (FileUtil.saveFile(inputStream, outputStream) && imageListener != null) {
imageListener.onSuccess(saveFile);
}
} catch (Exception e) {
if (imageListener != null) {
imageListener.onFail(e);
}
e.printStackTrace();
} finally {
imageReference.close();
closeableReference.close();
}
}
}
@Override
public void onProgressUpdate(DataSource<CloseableReference<PooledByteBuffer>> dataSource) {
int progress = (int) (dataSource.getProgress() * 100);
RingLog.d("fresco下载图片进度:" + progress);
}
@Override
public void onFailureImpl(DataSource dataSource) {
Throwable throwable = dataSource.getFailureCause();
if (imageListener != null) {
imageListener.onFail(throwable);
}
}
}, Executors.newSingleThreadExecutor());
}
use of com.facebook.common.references.CloseableReference in project DevRing by LJYcoder.
the class FrescoManager method getBitmap.
@Override
public void getBitmap(Context context, String url, final ImageListener<Bitmap> imageListener) {
// 参考自https://github.com/hpdx/fresco-helper/blob/master/fresco-helper/src/main/java/com/facebook/fresco/helper/ImageLoader.java
Uri uri = Uri.parse(url);
ImagePipeline imagePipeline = Fresco.getImagePipeline();
ImageRequestBuilder builder = ImageRequestBuilder.newBuilderWithSource(uri);
ImageRequest imageRequest = builder.build();
DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, context);
dataSource.subscribe(new BaseDataSubscriber<CloseableReference<CloseableImage>>() {
@Override
public void onNewResultImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
if (!dataSource.isFinished()) {
return;
}
CloseableReference<CloseableImage> imageReference = dataSource.getResult();
if (imageReference != null) {
final CloseableReference<CloseableImage> closeableReference = imageReference.clone();
try {
CloseableImage closeableImage = closeableReference.get();
// 动图处理
if (closeableImage instanceof CloseableAnimatedImage) {
AnimatedImageResult animatedImageResult = ((CloseableAnimatedImage) closeableImage).getImageResult();
if (animatedImageResult != null && animatedImageResult.getImage() != null) {
int imageWidth = animatedImageResult.getImage().getWidth();
int imageHeight = animatedImageResult.getImage().getHeight();
Bitmap.Config bitmapConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = Bitmap.createBitmap(imageWidth, imageHeight, bitmapConfig);
animatedImageResult.getImage().getFrame(0).renderFrame(imageWidth, imageHeight, bitmap);
if (imageListener != null) {
imageListener.onSuccess(bitmap);
}
}
} else // 非动图处理
if (closeableImage instanceof CloseableBitmap) {
CloseableBitmap closeableBitmap = (CloseableBitmap) closeableImage;
Bitmap bitmap = closeableBitmap.getUnderlyingBitmap();
if (bitmap != null && !bitmap.isRecycled()) {
// https://github.com/facebook/fresco/issues/648
final Bitmap tempBitmap = bitmap.copy(bitmap.getConfig(), false);
if (imageListener != null) {
imageListener.onSuccess(tempBitmap);
}
}
}
} finally {
imageReference.close();
closeableReference.close();
}
}
}
@Override
public void onFailureImpl(DataSource dataSource) {
Throwable throwable = dataSource.getFailureCause();
if (imageListener != null) {
imageListener.onFail(throwable);
}
}
}, UiThreadImmediateExecutorService.getInstance());
}
use of com.facebook.common.references.CloseableReference in project lzc_app_lib by httplzc.
the class BigImgDataRequestHelper method getDataBitmap.
// 获取图片的bitmap
private void getDataBitmap(final WrapperUri uri) {
ImagePipeline imagePipeline = Fresco.getImagePipeline();
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri.getUri()).setResizeOptions(new ResizeOptions(ScreenData.widthPX, ScreenData.heightPX)).build();
DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(request, "");
dataSource.subscribe(new BaseDataSubscriber<CloseableReference<CloseableImage>>() {
@Override
protected void onNewResultImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
if (!dataSource.isFinished()) {
return;
}
CloseableReference<CloseableImage> ref = dataSource.getResult();
if (ref != null && ref.isValid()) {
try {
data.put(uri.getUri(), ref.clone());
failSet.remove(uri.getUri());
if (imgDataLoadCompleteListener != null)
imgDataLoadCompleteListener.onRealDataLoadComplete(uri);
if (holderData.get(uri.getLowUri()) != null) {
CloseableReference refHolder = holderData.get(uri.getLowUri());
CloseableReference.closeSafely(refHolder);
holderData.remove(uri.getLowUri());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
CloseableReference.closeSafely(ref);
}
}
}
@Override
protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
if (holderData.get(uri.getLowUri()) != null) {
CloseableReference refHolder = holderData.get(uri.getLowUri());
CloseableReference.closeSafely(refHolder);
holderData.remove(uri.getLowUri());
}
if (imgDataLoadCompleteListener != null)
imgDataLoadCompleteListener.onRealDataFail(uri);
failSet.add(uri.getUri());
}
}, UiThreadImmediateExecutorService.getInstance());
}
Aggregations