Search in sources :

Example 1 with ImagePipeline

use of com.facebook.imagepipeline.core.ImagePipeline 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());
    }
}
Also used : ImageRequest(com.facebook.imagepipeline.request.ImageRequest) CloseableReference(com.facebook.common.references.CloseableReference) ImagePipeline(com.facebook.imagepipeline.core.ImagePipeline) File(java.io.File)

Example 2 with ImagePipeline

use of com.facebook.imagepipeline.core.ImagePipeline 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 3 with ImagePipeline

use of com.facebook.imagepipeline.core.ImagePipeline 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());
}
Also used : CloseableAnimatedImage(com.facebook.imagepipeline.image.CloseableAnimatedImage) DiskCacheConfig(com.facebook.cache.disk.DiskCacheConfig) ImageConfig(com.ljy.devring.image.support.ImageConfig) ImagePipelineConfig(com.facebook.imagepipeline.core.ImagePipelineConfig) SimpleProgressiveJpegConfig(com.facebook.imagepipeline.decoder.SimpleProgressiveJpegConfig) CloseableReference(com.facebook.common.references.CloseableReference) AnimatedImageResult(com.facebook.imagepipeline.animated.base.AnimatedImageResult) CloseableImage(com.facebook.imagepipeline.image.CloseableImage) Uri(android.net.Uri) CloseableBitmap(com.facebook.imagepipeline.image.CloseableBitmap) DataSource(com.facebook.datasource.DataSource) CloseableBitmap(com.facebook.imagepipeline.image.CloseableBitmap) Bitmap(android.graphics.Bitmap) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) ImagePipeline(com.facebook.imagepipeline.core.ImagePipeline) ImageRequestBuilder(com.facebook.imagepipeline.request.ImageRequestBuilder)

Example 4 with ImagePipeline

use of com.facebook.imagepipeline.core.ImagePipeline 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());
}
Also used : ResizeOptions(com.facebook.imagepipeline.common.ResizeOptions) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) CloseableReference(com.facebook.common.references.CloseableReference) ImagePipeline(com.facebook.imagepipeline.core.ImagePipeline)

Example 5 with ImagePipeline

use of com.facebook.imagepipeline.core.ImagePipeline in project lzc_app_lib by httplzc.

the class FunUntil method clearFrascoCache.

/**
 * 清除图片缓存
 */
public static void clearFrascoCache() {
    ImagePipeline imagePipeline = Fresco.getImagePipeline();
    imagePipeline.clearCaches();
}
Also used : ImagePipeline(com.facebook.imagepipeline.core.ImagePipeline)

Aggregations

ImagePipeline (com.facebook.imagepipeline.core.ImagePipeline)22 ImageRequest (com.facebook.imagepipeline.request.ImageRequest)17 CloseableReference (com.facebook.common.references.CloseableReference)16 Bitmap (android.graphics.Bitmap)10 DataSource (com.facebook.datasource.DataSource)10 BaseBitmapDataSubscriber (com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber)8 Uri (android.net.Uri)7 ImageRequestBuilder (com.facebook.imagepipeline.request.ImageRequestBuilder)5 CloseableImage (com.facebook.imagepipeline.image.CloseableImage)4 CloseableBitmap (com.facebook.imagepipeline.image.CloseableBitmap)3 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 SuppressLint (android.annotation.SuppressLint)2 ComponentName (android.content.ComponentName)2 RemoteViews (android.widget.RemoteViews)2 LatLng (com.amap.api.maps.model.LatLng)2 BinaryResource (com.facebook.binaryresource.BinaryResource)2 FileBinaryResource (com.facebook.binaryresource.FileBinaryResource)2 DiskCacheConfig (com.facebook.cache.disk.DiskCacheConfig)2 PooledByteBuffer (com.facebook.common.memory.PooledByteBuffer)2