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());
}
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);
}
}
Aggregations