• 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));
        }
    }
  • 相关阅读:
    线程同步 –Mutex和Semaphore
    线程同步 –AutoResetEvent和ManualResetEvent
    线程同步 – lock和Monitor
    .NET垃圾回收 – 非托管资源
    .NET垃圾回收 – 原理浅析
    反射简介—C#特性和反射
    反射简介—类型反射和晚期绑定
    Django REST framework 第一章 Serialization
    Django REST framework 简介
    Python Django 实用小案例2
  • 原文地址:https://www.cnblogs.com/csonezp/p/5124270.html
Copyright © 2020-2023  润新知