国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

android中Glide實現加載圖片保存至本地并加載回調監聽

2019-10-22 18:28:17
字體:
來源:轉載
供稿:網友

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[]> android/141092.html">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);

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


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 北辰区| 宁德市| 五大连池市| 金坛市| 昔阳县| 宁都县| 乌拉特中旗| 岚皋县| 南皮县| 冕宁县| 忻州市| 龙川县| 英吉沙县| 浠水县| 永城市| 林州市| 自贡市| 禄劝| 庆安县| 上虞市| 无极县| 四会市| 莱阳市| 磐石市| 左贡县| 常山县| 黎城县| 睢宁县| 商城县| 神池县| 灵台县| 阿拉善盟| 保亭| 呼图壁县| 香河县| 富阳市| 甘肃省| 蓬莱市| 教育| 庆阳市| 安福县|