use of com.facebook.common.references.CloseableReference in project boxing by Bilibili.
the class BoxingFrescoLoader method displayThumbnail.
@Override
public void displayThumbnail(@NonNull final ImageView img, @NonNull final String absPath, int width, int height) {
String finalAbsPath = "file://" + absPath;
ImageRequestBuilder requestBuilder = ImageRequestBuilder.newBuilderWithSource(Uri.parse(finalAbsPath));
requestBuilder.setResizeOptions(new ResizeOptions(width, height));
ImageRequest request = requestBuilder.build();
final DataSource<CloseableReference<CloseableImage>> dataSource = Fresco.getImagePipeline().fetchDecodedImage(request, null);
dataSource.subscribe(new BaseDataSubscriber<CloseableReference<CloseableImage>>() {
@Override
protected void onNewResultImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
String path = (String) img.getTag(R.string.boxing_app_name);
if (path == null || absPath.equals(path)) {
if (dataSource.getResult() == null) {
onFailureImpl(dataSource);
return;
}
Drawable drawable = createDrawableFromFetchedResult(img.getContext(), dataSource.getResult().get());
img.setImageDrawable(drawable);
}
}
@Override
protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
img.setImageResource(R.drawable.ic_boxing_broken_image);
}
}, UiThreadImmediateExecutorService.getInstance());
}
use of com.facebook.common.references.CloseableReference in project SherlockAdapter by EvilBT.
the class FrescoUtil method save.
public static Boolean save(@NonNull String url, @NonNull File outputFile) throws IOException {
ImagePipelineFactory factory = Fresco.getImagePipelineFactory();
ImagePipeline pipeline = factory.getImagePipeline();
boolean isInCache = pipeline.isInDiskCacheSync(Uri.parse(url));
if (isInCache) {
BinaryResource resource = factory.getMainFileCache().getResource(new SimpleCacheKey(url));
if (resource instanceof FileBinaryResource) {
FileBinaryResource fileResource = (FileBinaryResource) resource;
FileChannel input = new FileInputStream(fileResource.getFile()).getChannel();
FileChannel output = new FileOutputStream(outputFile).getChannel();
output.transferFrom(input, 0, input.size());
input.close();
output.close();
return true;
}
}
boolean isMemoryCache = pipeline.isInBitmapMemoryCache(Uri.parse(url));
if (!isMemoryCache) {
return false;
}
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url)).build();
DataSource<CloseableReference<CloseableImage>> dataSource = pipeline.fetchImageFromBitmapCache(request, null);
if (!dataSource.isFinished()) {
return false;
}
CloseableReference<CloseableImage> closeableImageRef = dataSource.getResult();
Bitmap bitmap = null;
if (closeableImageRef != null && closeableImageRef.get() instanceof CloseableBitmap) {
bitmap = ((CloseableBitmap) closeableImageRef.get()).getUnderlyingBitmap();
}
if (bitmap == null) {
return false;
}
FileOutputStream outputStream = new FileOutputStream(outputFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
return true;
}
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 fresco by facebook.
the class ImagePipelineTest method testLocalRequestListenerIsCalled.
@Test
public void testLocalRequestListenerIsCalled() {
RequestListener localRequestListner = mock(RequestListener.class);
when(mImageRequest.getRequestListener()).thenReturn(localRequestListner);
Producer<CloseableReference<CloseableImage>> bitmapCacheSequence = mock(Producer.class);
when(mProducerSequenceFactory.getDecodedImageProducerSequence(mImageRequest)).thenReturn(bitmapCacheSequence);
mImagePipeline.fetchImageFromBitmapCache(mImageRequest, mCallerContext);
verify(localRequestListner).onRequestStart(mImageRequest, mCallerContext, "0", false);
verify(mRequestListener1).onRequestStart(mImageRequest, mCallerContext, "0", false);
verify(mRequestListener2).onRequestStart(mImageRequest, mCallerContext, "0", false);
}
use of com.facebook.common.references.CloseableReference in project fresco by facebook.
the class BitmapPrepareProducerTest method setup.
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mImageReference = CloseableReference.of((CloseableImage) mCloseableStaticBitmap);
when(mCloseableStaticBitmap.getUnderlyingBitmap()).thenReturn(mBitmap);
// 100 * 15 = 1500 (between MIN_BITMAP_SIZE_BYTES and MAX_BITMAP_SIZE_BYTES)
when(mBitmap.getRowBytes()).thenReturn(100);
when(mBitmap.getHeight()).thenReturn(15);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
Consumer<CloseableReference<CloseableImage>> consumer = (Consumer<CloseableReference<CloseableImage>>) args[0];
consumer.onNewResult(mImageReference, 0);
return null;
}
}).when(mInputProducer).produceResults(any(Consumer.class), any(ProducerContext.class));
}
Aggregations