java.net.MalformedURLException: no protocol--Glide

no protocol翻译为无协议,像http/https都是最常用的指定协议,网络请求url格式不规范,就会造成该exception。
打印log:/storage/emulated/......的确是无协议,但是我就是加载本地图片的啊。
Glide图片加载库不仅可以加载网络资源也可以加载本地资源图片,但是为什么?

 public static void displayRound(Context context, ImageView imageView, String url) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        String cookie = SharedPreferencesUtil.getInstance().getString("cookie");
        GlideUrl glideUrl = new GlideUrl(url, new LazyHeaders.Builder().addHeader("Set-Cookie", cookie).build());
        RequestOptions  requestOptions = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.mipmap.ic_default_head)
                .error(R.mipmap.ic_default_head)
                .centerCrop()
                .fallback(new ColorDrawable(Color.RED))
                .transform(new GlideRoundTransformUtil())//圆角
        ;


        Glide.with(context).load(glideUrl)
                .apply(requestOptions)
                .into(imageView);

    }

简单看了看加载图片的代码,

 GlideUrl glideUrl = new GlideUrl(url, new LazyHeaders.Builder().addHeader("Set-Cookie", cookie).build());

本公司后台需要保持个session会话,因为gilde不添加session会被拒绝访问,这段代码就是给glide添加session用的。

Glide.with(context).load(glideUrl)
                .apply(requestOptions)
                .into(imageView);

这里直接load(glideUrl),默认该请求为网络请求,所以加载本地图片资源时报了no protocol
重新起个方法加载就好:

 public static void displayLocalPic(Context context, ImageView imageView, String url) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }

        RequestOptions   requestOptions = new RequestOptions()
                .placeholder(new ColorDrawable(Color.BLACK))
                .error(new ColorDrawable(Color.BLUE))
                .centerCrop();

        Glide.with(context).load(url)
                .apply(requestOptions)
                .into(imageView);

    }

猜你喜欢

转载自blog.csdn.net/julystroy/article/details/84986717