How to save image to sdcard when using Fresco?

独自空忆成欢 提交于 2019-12-10 17:45:34

问题


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


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/30751577/how-to-save-image-to-sdcard-when-using-fresco

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!