3

I am using Fresco to download and display Gifs in my app. I want to save the image to sdcard when click it, but i can't figure out how to do it.

final View view = inflater.inflate(R.layout.fragment_gif_viewer, container, false);
SimpleDraweeView draweeView = (SimpleDraweeView) view.findViewById(R.id.image);
Uri uri = Uri.parse(imageUrl);
DraweeController controller = Fresco.newDraweeControllerBuilder()
        .setUri(uri)
        .setAutoPlayAnimations(true)
        .build();
draweeView.setController(controller);

draweeView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Save the gif to /sdcard/test.gif
    }
});

I try to get the bitmap from the SimpleDraweeView following the instruction of How do I save an ImageView as an image?, but it's getDrawingCache() returns null

Community
  • 1
  • 1
dss886
  • 51
  • 1
  • 6

2 Answers2

2

You can do like this

ImageRequest downloadRequest = ImageRequest.fromUri(uri);
CacheKey cacheKey = DefaultCacheKeyFactory.getInstance().getEncodedCacheKey(downloadRequest);
if (ImagePipelineFactory.getInstance().getMainDiskStorageCache().hasKey(cacheKey)) {
    BinaryResource resource = ImagePipelineFactory.getInstance().getMainDiskStorageCache().getResource(cacheKey);
    File cacheFile = ((FileBinaryResource) resource).getFile();
    FileInputStream fis = new FileInputStream(cacheFile);
    ImageFormat imageFormat = ImageFormatChecker.getImageFormat(fis);
    switch (imageFormat) {
        case GIF:
        //copy cacheFile to sdcard
        break;
    }
}
s1rius
  • 846
  • 6
  • 9
  • 1
    did you test this answer? it fails with nullpointer exception – Edijae Crusar Feb 03 '16 at 08:20
  • It works on my project.Make sure this GIF is fetched on disk cache. – s1rius Feb 06 '16 at 03:11
  • 1
    The API changed you should use ```getMainFileCache()``` instead of ```getMainDiskStorageCache()```. This will return the whole cache. However this will give you the stored file in the Fresco cache rather than a file that you can display. – pranavrao1 Sep 04 '18 at 20:29
0

You can use the image pipeline directly to extract your GIF from disk cache.

Then you can use Java File methods to write it to the file system.

tyronen
  • 2,558
  • 1
  • 11
  • 15