I want to allow users to save GIF images in our app. I am trying to get GIF image from Fresco cache via fetchImageFromBitmapCache()
but it always returns NULL for GIF images.
For JPG and PNG images there is no problem.
Did you add compile 'com.facebook.fresco:animated-gif:1.0.1'
to your build.gradle
?
Yes I have that line.
compile ('com.facebook.fresco:animated-gif:1.0.1') {
exclude module: 'bolts-android'
}
I guess you have to use fetchDecodedImage
instead since animated images are handled a bit differently than normal static bitmaps (not in the same bitmap cache currently).
However, if you want to allow people to save images, you should get the encoded image instead of the decoded image, i.e. fetchEncodedImage
.
Thanks, it worked. I managed to save files with this code:
DataSource<CloseableReference<PooledByteBuffer>>
dataSource = Fresco.getImagePipeline().fetchEncodedImage(ImageRequest.fromUri(imageUrl), App.getContext());
DataSubscriber<CloseableReference<PooledByteBuffer>> dataSubscriber =
new BaseDataSubscriber<CloseableReference<PooledByteBuffer>>() {
@Override
protected void onNewResultImpl(
DataSource<CloseableReference<PooledByteBuffer>> dataSource) {
if (!dataSource.isFinished()) {
return;
}
CloseableReference<PooledByteBuffer> ref = dataSource.getResult();
if (ref != null) {
try {
PooledByteBuffer pooledByteBuffer = ref.get();
PooledByteBufferInputStream sourceIs = new PooledByteBufferInputStream(pooledByteBuffer);
BufferedInputStream bis = new BufferedInputStream(sourceIs);
String newFilePath = getAppFolderPath() + UUID.randomUUID().toString().toLowerCase() + "_" + URLUtil.guessFileName(imageUrl, null, null);
File targetFile = new File(newFilePath);
createDirectories(targetFile.getPath());
try {
BufferedOutputStream fout = new BufferedOutputStream(new FileOutputStream(newFilePath));
int i;
do {
i = bis.read();
if (i != -1)
fout.write(i);
} while (i != -1);
bis.close();
fout.close();
registerMediaInDeviceGallery(new File(newFilePath));
showMessage(R.string.saved_to_gallery);
} catch (IOException e) {
targetFile.delete();
e.printStackTrace();
showMessage(R.string.failed_to_save_to_gallery);
}
} finally {
CloseableReference.closeSafely(ref);
}
}
}
@Override
protected void onFailureImpl(DataSource<CloseableReference<PooledByteBuffer>> dataSource) {
Throwable t = dataSource.getFailureCause();
Log.e(TAG, "onFailureImpl: ", t);
showMessage(R.string.failed_to_save_to_gallery);
}
};
dataSource.subscribe(dataSubscriber, Runnable::run);
Most helpful comment
Thanks, it worked. I managed to save files with this code: