Search in sources :

Example 11 with DataSource

use of com.facebook.datasource.DataSource 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 12 with DataSource

use of com.facebook.datasource.DataSource in project fresco by facebook.

the class ImagePipelinePrefetchFragment method onViewCreated.

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mUris = new Uri[] { sampleUris().createSampleUri(ImageSize.L, Orientation.LANDSCAPE), sampleUris().createSampleUri(ImageSize.L, Orientation.PORTRAIT) };
    final Button clearCacheButton = (Button) view.findViewById(R.id.clear_cache);
    clearCacheButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            for (Uri uri : mUris) {
                Fresco.getImagePipeline().evictFromCache(uri);
            }
        }
    });
    mPrefetchStatus = (TextView) view.findViewById(R.id.prefetch_status);
    mPrefetchDiskButton = (Button) view.findViewById(R.id.prefetch_disk_now);
    mPrefetchDiskButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            mPrefetchDiskButton.setEnabled(false);
            final PrefetchSubscriber subscriber = new PrefetchSubscriber();
            for (Uri uri : mUris) {
                final DataSource<Void> ds = Fresco.getImagePipeline().prefetchToDiskCache(ImageRequest.fromUri(uri), null);
                ds.subscribe(subscriber, UiThreadImmediateExecutorService.getInstance());
            }
        }
    });
    mPrefetchEncodedButton = (Button) view.findViewById(R.id.prefetch_encoded_now);
    mPrefetchEncodedButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            mPrefetchEncodedButton.setEnabled(false);
            final PrefetchSubscriber subscriber = new PrefetchSubscriber();
            for (Uri uri : mUris) {
                final DataSource<Void> ds = Fresco.getImagePipeline().prefetchToEncodedCache(ImageRequest.fromUri(uri), null);
                ds.subscribe(subscriber, UiThreadImmediateExecutorService.getInstance());
            }
        }
    });
    mPrefetchBitmapButton = (Button) view.findViewById(R.id.prefetch_bitmap_now);
    mPrefetchBitmapButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            mPrefetchBitmapButton.setEnabled(false);
            final PrefetchSubscriber subscriber = new PrefetchSubscriber();
            for (Uri uri : mUris) {
                final DataSource<Void> ds = Fresco.getImagePipeline().prefetchToBitmapCache(ImageRequest.fromUri(uri), null);
                ds.subscribe(subscriber, UiThreadImmediateExecutorService.getInstance());
            }
        }
    });
    mDraweesHolder = (ViewGroup) view.findViewById(R.id.drawees);
    Button toggleImages = (Button) view.findViewById(R.id.toggle_images);
    toggleImages.setOnClickListener(new View.OnClickListener() {

        private boolean mShowing = false;

        @Override
        public void onClick(View v) {
            if (!mShowing) {
                for (int i = 0; i < mDraweesHolder.getChildCount(); i++) {
                    SimpleDraweeView draweeView = (SimpleDraweeView) mDraweesHolder.getChildAt(i);
                    draweeView.setController(Fresco.newDraweeControllerBuilder().setOldController(draweeView.getController()).setImageOriginListener(mImageOriginListener).setUri(mUris[i]).build());
                }
            } else {
                for (int i = 0; i < mDraweesHolder.getChildCount(); i++) {
                    ((SimpleDraweeView) mDraweesHolder.getChildAt(i)).setController(null);
                }
            }
            mShowing = !mShowing;
        }
    });
}
Also used : SimpleDraweeView(com.facebook.drawee.view.SimpleDraweeView) Button(android.widget.Button) TextView(android.widget.TextView) SimpleDraweeView(com.facebook.drawee.view.SimpleDraweeView) View(android.view.View) Uri(android.net.Uri) DataSource(com.facebook.datasource.DataSource)

Example 13 with DataSource

use of com.facebook.datasource.DataSource in project BigImageViewer by Piasy.

the class FrescoImageLoader method cancelAll.

@Override
public synchronized void cancelAll() {
    List<DataSource> sources = new ArrayList<>(mFlyingRequestSources.values());
    mFlyingRequestSources.clear();
    for (DataSource source : sources) {
        closeSource(source);
    }
    List<File> tempFiles = new ArrayList<>(mCacheMissTempFiles.values());
    mCacheMissTempFiles.clear();
    for (File tempFile : tempFiles) {
        deleteTempFile(tempFile);
    }
}
Also used : ArrayList(java.util.ArrayList) File(java.io.File) DataSource(com.facebook.datasource.DataSource)

Example 14 with DataSource

use of com.facebook.datasource.DataSource in project react-native-push-notification by zo0r.

the class RNPushNotificationPicturesAggregator method setBigPictureUrl.

public void setBigPictureUrl(Context context, String url) {
    if (null == url) {
        this.setBigPicture(null);
        return;
    }
    Uri uri = null;
    try {
        uri = Uri.parse(url);
    } catch (Exception ex) {
        Log.e(LOG_TAG, "Failed to parse bigPictureUrl", ex);
        this.setBigPicture(null);
        return;
    }
    final RNPushNotificationPicturesAggregator aggregator = this;
    this.downloadRequest(context, uri, new BaseBitmapDataSubscriber() {

        @Override
        public void onNewResultImpl(@Nullable Bitmap bitmap) {
            aggregator.setBigPicture(bitmap);
        }

        @Override
        public void onFailureImpl(DataSource dataSource) {
            aggregator.setBigPicture(null);
        }
    });
}
Also used : Bitmap(android.graphics.Bitmap) Uri(android.net.Uri) BaseBitmapDataSubscriber(com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber) DataSource(com.facebook.datasource.DataSource)

Example 15 with DataSource

use of com.facebook.datasource.DataSource in project react-native-push-notification by zo0r.

the class RNPushNotificationPicturesAggregator method setBigLargeIconUrl.

public void setBigLargeIconUrl(Context context, String url) {
    if (null == url) {
        this.setBigLargeIcon(null);
        return;
    }
    Uri uri = null;
    try {
        uri = Uri.parse(url);
    } catch (Exception ex) {
        Log.e(LOG_TAG, "Failed to parse bigLargeIconUrl", ex);
        this.setBigLargeIcon(null);
        return;
    }
    final RNPushNotificationPicturesAggregator aggregator = this;
    this.downloadRequest(context, uri, new BaseBitmapDataSubscriber() {

        @Override
        public void onNewResultImpl(@Nullable Bitmap bitmap) {
            aggregator.setBigLargeIcon(bitmap);
        }

        @Override
        public void onFailureImpl(DataSource dataSource) {
            aggregator.setBigLargeIcon(null);
        }
    });
}
Also used : Bitmap(android.graphics.Bitmap) Uri(android.net.Uri) BaseBitmapDataSubscriber(com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber) DataSource(com.facebook.datasource.DataSource)

Aggregations

DataSource (com.facebook.datasource.DataSource)15 Bitmap (android.graphics.Bitmap)11 Uri (android.net.Uri)10 ImagePipeline (com.facebook.imagepipeline.core.ImagePipeline)10 ImageRequest (com.facebook.imagepipeline.request.ImageRequest)10 CloseableReference (com.facebook.common.references.CloseableReference)9 BaseBitmapDataSubscriber (com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber)9 ImageRequestBuilder (com.facebook.imagepipeline.request.ImageRequestBuilder)5 CloseableImage (com.facebook.imagepipeline.image.CloseableImage)3 ComponentName (android.content.ComponentName)2 RemoteViews (android.widget.RemoteViews)2 LatLng (com.amap.api.maps.model.LatLng)2 DiskCacheConfig (com.facebook.cache.disk.DiskCacheConfig)2 PooledByteBuffer (com.facebook.common.memory.PooledByteBuffer)2 PooledByteBufferInputStream (com.facebook.common.memory.PooledByteBufferInputStream)2 AnimatedImageResult (com.facebook.imagepipeline.animated.base.AnimatedImageResult)2 ImagePipelineConfig (com.facebook.imagepipeline.core.ImagePipelineConfig)2 SimpleProgressiveJpegConfig (com.facebook.imagepipeline.decoder.SimpleProgressiveJpegConfig)2 CloseableAnimatedImage (com.facebook.imagepipeline.image.CloseableAnimatedImage)2 CloseableBitmap (com.facebook.imagepipeline.image.CloseableBitmap)2