亲宝软件园·资讯

展开

Android缓存大图到SD卡

_万能的博哥 人气:0

该功能主要针对资源图片过大占用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();
    }
  }

加载全部内容

相关教程
猜你喜欢
用户评论