使用Fresco时如何将图像保存到SD卡?

时间:2015-06-10 08:53:56

标签: android bitmap fresco

我正在使用Fresco在我的应用中下载并显示Gif。我想点击它时将图像保存到SD卡,但我无法弄清楚如何操作。

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

我尝试按照How do I save an ImageView as an image?的说明从SimpleDraweeView获取位图,但它的getDrawingCache()返回null

2 个答案:

答案 0 :(得分:2)

你可以这样做

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

答案 1 :(得分:0)

您可以use the image pipeline directly从磁盘缓存中提取GIF。

然后,您可以使用Java File方法将其写入文件系统。