Glide 下载Gif文件

之前做了一个类似朋友圈里的查看大图功能,现在也要加上保存功能。

保存图片有很多思路,可以从imageview里提取bitmap,可以用url下载到本地。imageview提取的话,gif图就会变成一张静态图。

我的图片使用glide加载的,glide自带缓存,如果再次用glide下载图片,则会直接读取缓存,节省时间。这次用的原理就是在点击保存图片的时候,将glide缓存文件保存到Pictures文件夹。

/**
 * Created by csonezp on 16-1-12.
 */
public class SaveImageTask extends AsyncTask<String, Void, File> {
    private
    final Context context;

    public SaveImageTask(Context context) {
        this.context = context;
    }

    @Override
    protected File doInBackground(String... params) {
        String url = params[0]; // should be easy to extend to share multiple images at once
        try {
            return Glide
                    .with(context)
                    .load(url)
                    .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                    .get() // needs to be called on background thread
                    ;
        } catch (Exception ex) {
            return null;
        }
    }

    @Override
    protected void onPostExecute(File result) {
        if (result == null) {
            return;
        }
        String path = result.getPath();
        FileUtil.copyFile(path, FileUtil.getPubAlbumDir().getPath() + UUID.randomUUID().toString() + ".gif");
        GlobalUtil.shortToast(context, context.getString(R.string.save_success));
    }
}
原文地址:https://www.cnblogs.com/csonezp/p/5124270.html