Android实现缓存大图到SD卡

作者:_万能的博哥 时间:2022-07-13 07:13:50 

本文实例为大家分享了Android实现缓存大图到SD卡的具体代码,供大家参考,具体内容如下

该功能主要针对资源图片过大占用apk体积,所以先将图片事先下载,在通过Glide加载时先去本地取,取值成功时直接应用且节省了时间,若本地图片不存在或取值失败等,在通过网络加载。。。

1、开启子线程
2、通过图片url进行本地缓存
3、判断SD是否挂载
4、判断本地是否存在该文件
5、存在将文件放到指定路径下 


public void downloadOnly(@Nullable final List<String> imageUrlList) {

if (Tools.isEmpty(imageUrlList)) {
     return;
   }

if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
     return;
   }

new Thread(new Runnable() {
     @Override
     public void run() {
       final File parent = MainApplication.getContext().getExternalCacheDir();
       for (String url : imageUrlList) {
         try {
           File tempFile = findImageByUrl(url, Tools.getApplication());
           if (tempFile == null) {
             File file = Glide
                 .with(MainApplication.getContext())
                 .load(url)
                 .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                 .get();
             Uri uri = Uri.parse(url);
             String fileName = uri.getLastPathSegment();
             if (Tools.notEmpty(fileName)) {
               copy(file, new File(parent, uri.getLastPathSegment()));
             }
           }
         } catch (Exception e) {
           e.printStackTrace();
         }
       }
     }
   }).start();
 }

//复制文件
 public void copy(File source, File target) {
   FileInputStream fileInputStream = null;
   FileOutputStream fileOutputStream = null;
   try {
     fileInputStream = new FileInputStream(source);
     fileOutputStream = new FileOutputStream(target);
     byte[] buffer = new byte[1024];
     while (fileInputStream.read(buffer) > 0) {
       fileOutputStream.write(buffer);
     }
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     try {
       if (fileInputStream != null) {
         fileInputStream.close();
       }

if (fileOutputStream != null) {
         fileOutputStream.close();
       }
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
 }

1、判断SD是否挂载
2、判断文件URL是否为空
3、判断文件是否存在


//查找本地文件是否存在
 @Nullable
 public static File findImageByUrl(@Nullable String url, @Nullable Context context) {
   if (Tools.isEmpty(url) || context == null) {
     return null;
   }
   try {
     if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
       return null;
     }
     Uri uri = Uri.parse(url);
     String fileName = uri.getLastPathSegment();
     if (Tools.notEmpty(fileName)) {
       File file = new File(context.getExternalCacheDir(), fileName);
       return file.exists() ? file : null;
     }
   } catch (Exception e) {
     return null;
   }
   return null;
 }

如上流程操作后,网络稳定的情况下已经将文件下载到本地了,只需调用该方法加载即可,如若网络不稳定的没下载成功情况下也没事,glide会协助加载的!!!


/**
  * 加载图片
  * 先从缓存中根据url对应名称判断是否有图片
  */
 public static void loadImageByCacheFirst(Context context, String url, ImageView imageView) {
   try {
     if (context == null) {
       return;
     }

File file = findImageByUrl(url, context);
     if (file != null) {
       Glide.with(context).load(file).into(imageView);
     } else {
       Glide.with(context).load(url).into(imageView);
     }
   } catch (Throwable t) {
     t.printStackTrace();
   }
 }

来源:https://blog.csdn.net/weixin_43917449/article/details/115124864

标签:Android,缓存,SD卡
0
投稿

猜你喜欢

  • Spring Cloud中FeignClient实现文件上传功能

    2023-06-23 07:57:09
  • javax.persistence中@Column定义字段类型方式

    2021-12-03 21:21:44
  • 使用Jacoco获取 Java 程序的代码执行覆盖率的步骤详解

    2022-07-22 00:25:13
  • Unity实现弹球打砖块游戏

    2021-09-24 16:13:08
  • Java Map集合用法详解

    2023-08-01 14:47:01
  • IDEA 单元测试覆盖技巧分享

    2022-09-09 03:50:26
  • spring异步service中处理线程数限制详解

    2021-09-02 23:20:12
  • C# Winform中如何绘制动画示例详解

    2022-03-28 13:26:26
  • SpringBoot yaml中的数组类型取值方式

    2022-10-14 20:05:36
  • 详解SpringCloud Config配置中心

    2021-06-18 04:38:34
  • 举例详解用Java实现web分页功能的方法

    2021-07-27 10:50:19
  • Java解决约瑟夫问题代码实例

    2023-09-20 19:17:02
  • springboot整合mybatis实现数据库的更新批处理方式

    2023-11-29 07:08:37
  • java必学必会之线程(2)

    2023-11-09 10:22:35
  • javaWeb项目部署到阿里云服务器步骤详解

    2023-11-07 05:21:36
  • SpringMVC使用ResponseEntity实现文件上传下载

    2023-08-20 02:10:58
  • 快速解决Android适配底部返回键等虚拟键盘的问题

    2021-10-25 14:50:23
  • JavaWeb入门教程之分页查询功能的简单实现

    2021-11-11 21:52:23
  • 详解SpringBoot 快速整合MyBatis(去XML化)

    2022-08-19 16:42:54
  • java增强for循环的实现方法

    2023-12-07 16:42:53
  • asp之家 软件编程 m.aspxhome.com