Search in sources :

Example 31 with CloseableReference

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());
}
Also used : ResizeOptions(com.facebook.imagepipeline.common.ResizeOptions) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) CloseableReference(com.facebook.common.references.CloseableReference) Drawable(android.graphics.drawable.Drawable) AnimatedDrawable(com.facebook.imagepipeline.animated.base.AnimatedDrawable) OrientedDrawable(com.facebook.drawee.drawable.OrientedDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) ImageRequestBuilder(com.facebook.imagepipeline.request.ImageRequestBuilder)

Example 32 with CloseableReference

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;
}
Also used : ImagePipelineFactory(com.facebook.imagepipeline.core.ImagePipelineFactory) FileChannel(java.nio.channels.FileChannel) CloseableReference(com.facebook.common.references.CloseableReference) CloseableImage(com.facebook.imagepipeline.image.CloseableImage) FileBinaryResource(com.facebook.binaryresource.FileBinaryResource) FileInputStream(java.io.FileInputStream) CloseableBitmap(com.facebook.imagepipeline.image.CloseableBitmap) CloseableBitmap(com.facebook.imagepipeline.image.CloseableBitmap) Bitmap(android.graphics.Bitmap) BinaryResource(com.facebook.binaryresource.BinaryResource) FileBinaryResource(com.facebook.binaryresource.FileBinaryResource) FileOutputStream(java.io.FileOutputStream) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) ImagePipeline(com.facebook.imagepipeline.core.ImagePipeline)

Example 33 with CloseableReference

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());
}
Also used : PooledByteBufferInputStream(com.facebook.common.memory.PooledByteBufferInputStream) PooledByteBufferInputStream(com.facebook.common.memory.PooledByteBufferInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) CloseableReference(com.facebook.common.references.CloseableReference) Uri(android.net.Uri) DataSource(com.facebook.datasource.DataSource) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) FileOutputStream(java.io.FileOutputStream) PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) ImagePipeline(com.facebook.imagepipeline.core.ImagePipeline) ImageRequestBuilder(com.facebook.imagepipeline.request.ImageRequestBuilder)

Example 34 with CloseableReference

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);
}
Also used : RequestListener(com.facebook.imagepipeline.listener.RequestListener) CloseableReference(com.facebook.common.references.CloseableReference) Test(org.junit.Test)

Example 35 with CloseableReference

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));
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CloseableReference(com.facebook.common.references.CloseableReference) CloseableImage(com.facebook.imagepipeline.image.CloseableImage) Before(org.junit.Before)

Aggregations

CloseableReference (com.facebook.common.references.CloseableReference)50 ImageRequest (com.facebook.imagepipeline.request.ImageRequest)24 Bitmap (android.graphics.Bitmap)17 ImagePipeline (com.facebook.imagepipeline.core.ImagePipeline)16 CloseableImage (com.facebook.imagepipeline.image.CloseableImage)14 DataSource (com.facebook.datasource.DataSource)9 BaseBitmapDataSubscriber (com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber)8 Uri (android.net.Uri)7 CloseableStaticBitmap (com.facebook.imagepipeline.image.CloseableStaticBitmap)7 AnimatedImageResult (com.facebook.imagepipeline.animated.base.AnimatedImageResult)5 CloseableAnimatedImage (com.facebook.imagepipeline.image.CloseableAnimatedImage)5 ImageRequestBuilder (com.facebook.imagepipeline.request.ImageRequestBuilder)5 Nullable (javax.annotation.Nullable)5 Test (org.junit.Test)5 SuppressLint (android.annotation.SuppressLint)4 CacheKey (com.facebook.cache.common.CacheKey)3 MemoryTrimType (com.facebook.common.memory.MemoryTrimType)3 ResizeOptions (com.facebook.imagepipeline.common.ResizeOptions)3 CloseableBitmap (com.facebook.imagepipeline.image.CloseableBitmap)3 FakeClock (com.facebook.imagepipeline.testing.FakeClock)3