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