zoukankan      html  css  js  c++  java
  • v关于使用Glide加载图片失败时显示自己特定的图片

    Glide是Android加载图片的一个框架。

    常用加载图片到imageView:Glide.with(this).load(url).into(ImageView imageview).
    当加载失败时一般情侣下会默认调用Glide.error()方法,只需要在error方法中放一个默认图片即可,但当我们需要设置的默认图片是网络图片或者是手机本地图片时,这个方法行不通。
    解决办法:Glide为我们提供了listener()方法,onException是图片加载异常回调,将它设置为false时会调用Glide默认的error方法,需要将它设置为true,然后调用我们自己设置的图片加载异常处理,onResourceReady是加载成功的处理。

    1、首先给定一个你要获取图片路径
    final String path = Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"Anycctv"+File.separator+device.deviceUid+".jpg";//这是我自己的本地路径,换成你自己的
    
    2、使用listener方法进行监听
          DrawableTypeRequest<Uri> request = Glide.with(this).load(uri);
            request.listener(new RequestListener<Uri, GlideDrawable>() {
                @Override
                public boolean onException(Exception e, Uri model, Target<GlideDrawable> target, boolean isFirstResource) {
                    dismissLoading();
                    //showToast("ex="+e.getMessage());
                    Glide.with(UlifeplusApp.app).load(path)
                            .placeholder(mIvPicture.getDrawable())
                            .signature(new StringSignature(signature))
                            .crossFade(300)
                            .error(R.drawable.ic_fg_device_item)
                            .into(mIvPicture);
                    return true;
                }
    
                @Override
                public boolean onResourceReady(GlideDrawable resource, Uri model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                    dismissLoading();
                    //showToast("succ");
                    return false;
                }
            })
                    .crossFade()
                    .override(Utils.getRealSize(this).x, getResources().getDimensionPixelSize(R.dimen.w_480px))
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.RESULT)
                    .into(mIvPicture);
    

    小礼物走一走,来简书关注我

     
  • 相关阅读:
    Could note find result map com.xxxx.entity.UserAccountDO
    浏览器通过file://访问文件和通过http://访问文件有什么区别
    FreeMarker template error: The following has evaluated to null or missing: ==> blogger.md [in template "admin/about.ftl" at line 44, column 84]
    2018-01-03 --活动观赏鱼的2017总结及2018年展望
    eclipse open call hierarchy无效
    Mysql 查询重复的记录
    oracle用户、权限操作
    Oracle 修改表操作
    Python 魔法方法详解
    Python __dict__和vars()
  • 原文地址:https://www.cnblogs.com/yelanggu/p/10831510.html
Copyright © 2011-2022 走看看