Search in sources :

Example 21 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 22 with ImagePipeline

use of com.facebook.imagepipeline.core.ImagePipeline in project BigImageViewer by Piasy.

the class FrescoImageLoader method loadImage.

@SuppressLint("WrongThread")
@Override
public void loadImage(final int requestId, Uri uri, final Callback callback) {
    ImageRequest request = ImageRequest.fromUri(uri);
    final File localCache = getCacheFile(request);
    if (localCache.exists()) {
        mExecutorSupplier.forLocalStorageRead().execute(new Runnable() {

            @Override
            public void run() {
                callback.onCacheHit(ImageInfoExtractor.getImageType(localCache), localCache);
                callback.onSuccess(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(final File image) {
                rememberTempFile(requestId, image);
                callback.onFinish();
                callback.onCacheMiss(ImageInfoExtractor.getImageType(image), image);
                callback.onSuccess(image);
            }

            @Override
            protected void onFail(final Throwable t) {
                t.printStackTrace();
                callback.onFail((Exception) t);
            }
        }, mExecutorSupplier.forBackgroundTasks());
        cancel(requestId);
        rememberSource(requestId, source);
    }
}
Also used : ImageRequest(com.facebook.imagepipeline.request.ImageRequest) CloseableReference(com.facebook.common.references.CloseableReference) ImagePipeline(com.facebook.imagepipeline.core.ImagePipeline) File(java.io.File) SuppressLint(android.annotation.SuppressLint) SuppressLint(android.annotation.SuppressLint)

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