Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When trying to fetchImageFromBitmapCache() for GIF image, imageReference is always null #1645

Closed
rafaelekol opened this issue Jan 30, 2017 · 4 comments
Labels

Comments

@rafaelekol
Copy link

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.

@oprisnik
Copy link
Contributor

Did you add compile 'com.facebook.fresco:animated-gif:1.0.1' to your build.gradle?

@oprisnik oprisnik added the needs-details This issue or PR is currently not actionable as it misses details (e.g. for reproducing the problem) label Jan 30, 2017
@rafaelekol
Copy link
Author

Yes I have that line.

compile ('com.facebook.fresco:animated-gif:1.0.1') {
        exclude module: 'bolts-android'
    }

@oprisnik
Copy link
Contributor

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.

@oprisnik oprisnik added question and removed needs-details This issue or PR is currently not actionable as it misses details (e.g. for reproducing the problem) labels Jan 30, 2017
@rafaelekol
Copy link
Author

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants