How to save image to SD card when using Fresco?

I am using Fresco to load and display Gifs in my application. I want to save the image to the SD card when I click on it, but I can't figure out how.

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 am trying to get a bitmap from SimpleDraweeView according to the instruction How to save ImageView as image? but it getDrawingCache()

returns null

+3


source to share


2 answers


You can do it 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;
    }
}

      

+2


source


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



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

0


source







All Articles