欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android中Glide實現(xiàn)加載圖片保存至本地并加載回調監(jiān)聽

 更新時間:2017年09月08日 08:28:48   作者:code小生  
本篇文章主要介紹了android中Glide實現(xiàn)加載圖片保存至本地并加載回調監(jiān)聽,具有一定的參考價值,有興趣的可以了解一下

Glide 加載圖片使用到的兩個記錄

Glide 加載圖片保存至本地指定路徑

/**
     * Glide 加載圖片保存到本地
     *
     * imgUrl 圖片地址
     * imgName 圖片名稱
     */
    Glide.with(context).load(imgUrl).asBitmap().toBytes().into(new SimpleTarget<byte[]>() {
      @Override
      public void onResourceReady(byte[] bytes, GlideAnimation<? super byte[]> glideAnimation) {
        try {
          savaBitmap(imgName, bytes);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });

// 保存圖片到手機指定目錄
  public void savaBitmap(String imgName, byte[] bytes) {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
      String filePath = null;
      FileOutputStream fos = null;
      try {
        filePath = Environment.getExternalStorageDirectory().getCanonicalPath() + "/MyImg";
        File imgDir = new File(filePath);
        if (!imgDir.exists()) {
          imgDir.mkdirs();
        }
        imgName = filePath + "/" + imgName;
        fos = new FileOutputStream(imgName);
        fos.write(bytes);
        Toast.makeText(context, "圖片已保存到" + filePath, Toast.LENGTH_SHORT).show();
      } catch (IOException e) {
        e.printStackTrace();
      } finally {
        try {
          if (fos != null) {
            fos.close();
          }
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    } else {
      Toast.makeText(context, "請檢查SD卡是否可用", Toast.LENGTH_SHORT).show();
    }
  }

Glide 加載圖片回調方法

Glide.with(context).load(imgUrl)
        .listener(new RequestListener<String, GlideDrawable>() {
          @Override
          public boolean onException(Exception e, String model,
                        Target<GlideDrawable> target,
                        boolean isFirstResource) {
            // 可替換成進度條
            Toast.makeText(context, "圖片加載失敗", Toast.LENGTH_SHORT).show();
            return false;
          }

          @Override
          public boolean onResourceReady(GlideDrawable resource, String model,
                          Target<GlideDrawable> target,
                          boolean isFromMemoryCache,
                          boolean isFirstResource) {
            // 圖片加載完成,取消進度條
            Toast.makeText(context, "圖片加載成功", Toast.LENGTH_SHORT).show();
            return false;
          }
        }).error(R.mipmap.ic_launcher_round)
        .diskCacheStrategy(DiskCacheStrategy.ALL)
        .into(imageView);

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論