最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實(shí)現(xiàn)緩存大圖到SD卡

 更新時(shí)間:2021年09月27日 17:29:03   作者:_萬能的博哥  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)緩存大圖到SD卡,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android實(shí)現(xiàn)緩存大圖到SD卡的具體代碼,供大家參考,具體內(nèi)容如下

該功能主要針對(duì)資源圖片過大占用apk體積,所以先將圖片事先下載,在通過Glide加載時(shí)先去本地取,取值成功時(shí)直接應(yīng)用且節(jié)省了時(shí)間,若本地圖片不存在或取值失敗等,在通過網(wǎng)絡(luò)加載。。。

1、開啟子線程
2、通過圖片url進(jìn)行本地緩存
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();
  }
 
  //復(fù)制文件
  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;
  }

如上流程操作后,網(wǎng)絡(luò)穩(wěn)定的情況下已經(jīng)將文件下載到本地了,只需調(diào)用該方法加載即可,如若網(wǎng)絡(luò)不穩(wěn)定的沒下載成功情況下也沒事,glide會(huì)協(xié)助加載的!??!

 /**
   * 加載圖片
   * 先從緩存中根據(jù)url對(duì)應(yīng)名稱判斷是否有圖片
   */
  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();
    }
  }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

石林| 嘉定区| 抚宁县| 门头沟区| 定西市| 萨嘎县| 托克托县| 新余市| 台湾省| 斗六市| 潜江市| 财经| 连山| 台北县| 西乌珠穆沁旗| 望谟县| 巴马| 阳朔县| 广南县| 嘉峪关市| 万荣县| 房产| 和平区| 隆昌县| 河南省| 嵊泗县| 玛纳斯县| 西乡县| 昂仁县| 都昌县| 科技| 沂水县| 蓝田县| 蕲春县| 彰化市| 南召县| 西宁市| 丰镇市| 佛山市| 高陵县| 城市|