/** * Submits a request for execution and returns a DataSource representing the pending decoded * image(s). * <p>The returned DataSource must be closed once the client has finished with it. * * @param imageRequest the request to submit * @param callerContext the caller context for image request * @param lowestPermittedRequestLevelOnSubmit the lowest request level permitted for image request * @return a DataSource representing the pending decoded image(s) */ public DataSource<CloseableReference<CloseableImage>> fetchDecodedImage( ImageRequest imageRequest, Object callerContext, ImageRequest.RequestLevel lowestPermittedRequestLevelOnSubmit) { try { Producer<CloseableReference<CloseableImage>> producerSequence = mProducerSequenceFactory.getDecodedImageProducerSequence(imageRequest); return submitFetchRequest( producerSequence, imageRequest, lowestPermittedRequestLevelOnSubmit, callerContext); } catch (Exception exception) { return DataSources.immediateFailedDataSource(exception); } }
/** * Submits a request for prefetching to the bitmap cache. * * <p> Beware that if your network fetcher doesn't support priorities prefetch requests may slow * down images which are immediately required on screen. * * @param imageRequest the request to submit * @return a DataSource that can safely be ignored. */ public DataSource<Void> prefetchToBitmapCache( ImageRequest imageRequest, Object callerContext) { if (!mIsPrefetchEnabledSupplier.get()) { return DataSources.immediateFailedDataSource(PREFETCH_EXCEPTION); } try { Producer<Void> producerSequence = mSuppressBitmapPrefetchingSupplier.get() ? mProducerSequenceFactory.getEncodedImagePrefetchProducerSequence(imageRequest) : mProducerSequenceFactory.getDecodedImagePrefetchProducerSequence(imageRequest); return submitPrefetchRequest( producerSequence, imageRequest, ImageRequest.RequestLevel.FULL_FETCH, callerContext, Priority.MEDIUM); } catch (Exception exception) { return DataSources.immediateFailedDataSource(exception); } }
/** * Submits a request for prefetching to the disk cache. * * <p> Beware that if your network fetcher doesn't support priorities prefetch requests may slow * down images which are immediately required on screen. * * @param imageRequest the request to submit * @param priority custom priority for the fetch * @return a DataSource that can safely be ignored. */ public DataSource<Void> prefetchToDiskCache( ImageRequest imageRequest, Object callerContext, Priority priority) { if (!mIsPrefetchEnabledSupplier.get()) { return DataSources.immediateFailedDataSource(PREFETCH_EXCEPTION); } try { Producer<Void> producerSequence = mProducerSequenceFactory.getEncodedImagePrefetchProducerSequence(imageRequest); return submitPrefetchRequest( producerSequence, imageRequest, ImageRequest.RequestLevel.FULL_FETCH, callerContext, priority); } catch (Exception exception) { return DataSources.immediateFailedDataSource(exception); } }
/** * Submits a request for execution and returns a DataSource representing the pending encoded * image(s). * * <p> The ResizeOptions in the imageRequest will be ignored for this fetch * * <p>The returned DataSource must be closed once the client has finished with it. * * @param imageRequest the request to submit * @return a DataSource representing the pending encoded image(s) */ public DataSource<CloseableReference<PooledByteBuffer>> fetchEncodedImage( ImageRequest imageRequest, Object callerContext) { Preconditions.checkNotNull(imageRequest.getSourceUri()); try { Producer<CloseableReference<PooledByteBuffer>> producerSequence = mProducerSequenceFactory.getEncodedImageProducerSequence(imageRequest); // The resize options are used to determine whether images are going to be downsampled during // decode or not. For the case where the image has to be downsampled and it's a local image it // will be kept as a FileInputStream until decoding instead of reading it in memory. Since // this method returns an encoded image, it should always be read into memory. Therefore, the // resize options are ignored to avoid treating the image as if it was to be downsampled // during decode. if (imageRequest.getResizeOptions() != null) { imageRequest = ImageRequestBuilder.fromRequest(imageRequest) .setResizeOptions(null) .build(); } return submitFetchRequest( producerSequence, imageRequest, ImageRequest.RequestLevel.FULL_FETCH, callerContext); } catch (Exception exception) { return DataSources.immediateFailedDataSource(exception); } }
private <T> DataSource<CloseableReference<T>> submitFetchRequest( Producer<CloseableReference<T>> producerSequence, ImageRequest imageRequest, ImageRequest.RequestLevel lowestPermittedRequestLevelOnSubmit, Object callerContext) { final RequestListener requestListener = getRequestListenerForRequest(imageRequest); try { ImageRequest.RequestLevel lowestPermittedRequestLevel = ImageRequest.RequestLevel.getMax( imageRequest.getLowestPermittedRequestLevel(), lowestPermittedRequestLevelOnSubmit); SettableProducerContext settableProducerContext = new SettableProducerContext( imageRequest, generateUniqueFutureId(), requestListener, callerContext, lowestPermittedRequestLevel, /* isPrefetch */ false, imageRequest.getProgressiveRenderingEnabled() || imageRequest.getMediaVariations() != null || !UriUtil.isNetworkUri(imageRequest.getSourceUri()), imageRequest.getPriority()); return CloseableProducerToDataSourceAdapter.create( producerSequence, settableProducerContext, requestListener); } catch (Exception exception) { return DataSources.immediateFailedDataSource(exception); } }
private DataSource<Void> submitPrefetchRequest( Producer<Void> producerSequence, ImageRequest imageRequest, ImageRequest.RequestLevel lowestPermittedRequestLevelOnSubmit, Object callerContext, Priority priority) { final RequestListener requestListener = getRequestListenerForRequest(imageRequest); try { ImageRequest.RequestLevel lowestPermittedRequestLevel = ImageRequest.RequestLevel.getMax( imageRequest.getLowestPermittedRequestLevel(), lowestPermittedRequestLevelOnSubmit); SettableProducerContext settableProducerContext = new SettableProducerContext( imageRequest, generateUniqueFutureId(), requestListener, callerContext, lowestPermittedRequestLevel, /* isPrefetch */ true, /* isIntermediateResultExpected */ false, priority); return ProducerToDataSourceAdapter.create( producerSequence, settableProducerContext, requestListener); } catch (Exception exception) { return DataSources.immediateFailedDataSource(exception); } }
/** Gets the top-level data source supplier to be used by a controller. */ protected Supplier<DataSource<IMAGE>> obtainDataSourceSupplier() { if (mDataSourceSupplier != null) { return mDataSourceSupplier; } Supplier<DataSource<IMAGE>> supplier = null; // final image supplier; if (mImageRequest != null) { supplier = getDataSourceSupplierForRequest(mImageRequest); } else if (mMultiImageRequests != null) { supplier = getFirstAvailableDataSourceSupplier(mMultiImageRequests, mTryCacheOnlyFirst); } // increasing-quality supplier; highest-quality supplier goes first if (supplier != null && mLowResImageRequest != null) { List<Supplier<DataSource<IMAGE>>> suppliers = new ArrayList<>(2); suppliers.add(supplier); suppliers.add(getDataSourceSupplierForRequest(mLowResImageRequest)); supplier = IncreasingQualityDataSourceSupplier.create(suppliers); } // no image requests; use null data source supplier if (supplier == null) { supplier = DataSources.getFailedDataSourceSupplier(NO_REQUEST_EXCEPTION); } return supplier; }
@Override public BitmapRegionDecoder intercept(Chain chain) throws IOException { final Uri uri = chain.uri(); BitmapRegionDecoder decoder = chain.chain(uri); if (decoder != null){ return decoder; } if (UriUtil.isNetworkUri(uri)){ ImagePipeline imagePipeline = ImagePipelineFactory.getInstance().getImagePipeline(); ImageRequest request = ImageRequest.fromUri(uri); DataSource<CloseableReference<PooledByteBuffer>> dataSource = imagePipeline.fetchEncodedImage(request,null); try { CloseableReference<PooledByteBuffer> ref = DataSources.waitForFinalResult(dataSource); if (ref == null){ return null; } PooledByteBuffer result = ref.get(); if (BuildConfig.DEBUG) { Log.d("FrescoInterceptor", "从我这加载"); } try { InputStream inputStream = new PooledByteBufferInputStream(result); Closeables.closeQuietly(inputStream); return BitmapRegionDecoder.newInstance(inputStream,false); } catch (IOException e) { ImageRequest imageRequest=ImageRequest.fromUri(uri); CacheKey cacheKey= DefaultCacheKeyFactory.getInstance().getEncodedCacheKey(imageRequest,null); BinaryResource resource = ImagePipelineFactory.getInstance().getMainFileCache().getResource(cacheKey); File file=((FileBinaryResource)resource).getFile(); if (BuildConfig.DEBUG) { Log.d("FrescoInterceptor", file.getName()); } return Interceptors.fixJPEGDecoder(file,e); } } catch (Throwable throwable) { if (BuildConfig.DEBUG) { Log.d("FrescoInterceptor", "intercept: 加载失败了"); } throwable.printStackTrace(); return null; } } return null; }