question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use ImagePipeline.isInDiskCache(uri) in DraweeHolder

See original GitHub issue

In our app we use custom views to show Images. Everything works fine, but when I tried to use ImagePipeline.isInDiskCache(uri) wrong images started to set in wrong places. I use DraweeHolder to show the images. Here is my code:

public void draw(Canvas canvas) {
        Drawable drawable = draweeHolder.getTopLevelDrawable();
        drawable.setBounds(0, 0, width, height);
        drawable.draw(canvas);
    }
private void checkImageCache(String imgUrl, String thumbUrl) {
        ImagePipeline imagePipeline = Fresco.getImagePipeline();
        Uri uri = Uri.parse(imgUrl);

        DataSource<Boolean> inDiskCacheSource = imagePipeline.isInDiskCache(uri);
        DataSubscriber<Boolean> subscriber = new BaseDataSubscriber<Boolean>() {
            @Override
            protected void onNewResultImpl(DataSource<Boolean> dataSource) {
                if (!dataSource.isFinished()) {
                    return;
                }
                boolean isInCache = dataSource.getResult();
                if (isInCache) {
                    loadImage(imgUrl, finalThumbUrl);
                } else {
                    if (isAutoDownloadAllowed()) {
                        loadImage(imgUrl, thumbUrl);
                        draweeHolder.getHierarchy().setProgressBarImage(new CircleProgressBarDrawable());
                    } else {
                        loadThumb(thumbUrl);
                    }
                }
            }

            @Override
            protected void onFailureImpl(DataSource<Boolean> dataSource) {
                Log.d(TAG, "onFailureImpl: " + dataSource.getFailureCause());
            }
        };

        boolean inMemoryCache = imagePipeline.isInBitmapMemoryCache(uri);

        if (inMemoryCache) {
            loadImage(imgUrl, thumbUrl);
        } else {
            inDiskCacheSource.subscribe(subscriber, Runnable::run);
        }
    }

    private void loadImage(String imageUrl, String thumbUrl) {
        
        ControllerListener<ImageInfo> controllerListener = new BaseControllerListener<ImageInfo>() {
            @Override
            public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                parent.invalidate();
            }

            @Override
            public void onFailure(String id, Throwable throwable) {
                parent.invalidate();
                loadThumb(thumbUrl);
            }
        };

        PipelineDraweeControllerBuilder controllerBuilder = Fresco.newDraweeControllerBuilder()
                .setControllerListener(controllerListener)
                .setOldController(draweeHolder.getController())
                .setAutoPlayAnimations(true)
                .setImageRequest(ImageRequest.fromUri(imageUrl));

        draweeHolder.setController(controllerBuilder.build());
    }

    private void loadThumb(String thumbUrl) {
        ControllerListener<ImageInfo> controllerListener = new BaseControllerListener<ImageInfo>() {
            @Override
            public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                parent.invalidate();
            }

            @Override
            public void onFailure(String id, Throwable throwable) {
                parent.invalidate();
            }
        };

        DraweeController controllerBuilder = Fresco.newDraweeControllerBuilder()
                .setImageRequest(MediaHelper.getGifThumbnailRequest(thumbUrl))
                .setControllerListener(controllerListener)
                .setOldController(draweeHolder.getController())
                .build();

        draweeHolder.setController(controllerBuilder);
    }

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
eldkcommented, Jan 31, 2017

Hello,

If pictures are ordered, maybe you should use one ordered check and not an async one :

https://github.com/facebook/fresco/issues/603

This should slowered the cache check but gives pictures in right order.

Eric

0reactions
rafaelekolcommented, Feb 7, 2017

@oprisnik Thank you, I will look into that direction.

Read more comments on GitHub >

github_iconTop Results From Across the Web

com.facebook.imagepipeline.request.ImageRequest Example
Learn how to use java api com.facebook.imagepipeline.request. ... public static void load(Uri uri, SimpleDraweeView draweeView, BasePostprocessor processor, ...
Read more >
ImagePipeline - Fresco API
Removes all images with the specified Uri from memory cache. ... cache key entirely from the URI. If that is not the case,...
Read more >
第三方图片加载组件Fresco使用指南-51CTO.COM
内存缓存的检查是同步的: ImagePipeline imagePipeline = Fresco.getImagePipeline(); Uri uri; boolean inMemoryCache = imagePipeline.
Read more >
fresco清理过期 - CSDN
DataSource<Boolean> inDiskCacheSource = imagePipeline.isInDiskCache(uri); DataSubscriber<Boolean> subscriber = new BaseDataSubscriber<Boolean>() { @Override ...
Read more >
Fresco初識 - 台部落
Fresco包含Drawees、The Image Pipeline兩個部分。我們先了解一下他們。 ... isInDiskCache(uri); DataSubscriber<Boolean> subscriber = new ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found