Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

Android display images when offline once downloaded using fresco

I want fresco to download and save images to sd-card when connected to internet. Later when offline and even if cache is cleared, still I need fresco to show saved images. Is this possible? If yes, how?

Simply saving images to disk cache doesnt seem to work when cache is cleared.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Fresco caches images for you. If you are offline, the images should still be displayed. You should not need to do anything.

However, when the cache is cleared (e.g. when the user presses the button or when device space is low), images are obviously deleted from the cache - which is the desired behavior that should not be changed.

There are 2 options: save selected items, move the cache

Save selected items

If you want to persist selected images (e.g. a "Save" button), you can get the encoded image and save it somewhere on the device. You should not do this for all images since they will be on the disk 2 times and clearing the cache / uninstalling the app will leave 1 copy on the device.

Something like this could work:

DataSource<CloseableReference<PooledByteBuffer>>
    dataSource = Fresco.getImagePipeline().fetchEncodedImage(imageRequest, callerContext);
dataSource.subscribe(new BaseDataSubscriber<CloseableReference<PooledByteBuffer>>() {
  @Override
  protected void onNewResultImpl(DataSource<CloseableReference<PooledByteBuffer>> dataSource) {
    CloseableReference<PooledByteBuffer> encodedImage = dataSource.getResult();
    if (encodedImage != null) {
      try {
        // save the encoded image in the PooledByteBuffer
      } finally {
        CloseableReference.closeSafely(encodedImage);
      }
    }
  }

  @Override
  protected void onFailureImpl(DataSource<CloseableReference<PooledByteBuffer>> dataSource) {
    // something went wrong
  }
}, executorService);

}

More information on how to use the pipeline to get the encoded image: http://frescolib.org/docs/using-image-pipeline.html

Move the cache

Keep in mind that this will persist the cache when it is moved to an external directory, so be careful since this will leave files when the app is uninstalled.

Fresco also allows you to supply a custom DiskCacheConfig and you can create a new DiskCacheConfig.Builder and call setBaseDirectoryPath(File) to change the path to a different folder (e.g. one on the SD card) and you can also change the directory name with setBaseDirectoryName(String)

More information on how Fresco does caching: http://frescolib.org/docs/caching.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...