Glide图片库基本用法及加载认证的https图片
来源:互联网 发布:渥瑞达unity3d培训 编辑:程序博客网 时间:2024/05/19 20:00
Glide的基本用法
第一步:build.gradle中引入第三方Glide库
compile 'com.github.bumptech.glide:glide:latest.integration'
第二步:proguad-rule.pro混淆文件不进行混淆处理
#Glide-keep public class * implements com.bumptech.glide.module.GlideModule-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** { **[] $VALUES; public *;}
第三步:加载图片方式
public class ARGB8888 implements GlideModule { @Override public void applyOptions(Context context, GlideBuilder builder) { // Apply options to the builder here. builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888); } @Override public void registerComponents(Context context, Glide glide) { }}在AndroidManifest.xml
<meta-data android:name="包名.ARGB8888" android:value="GlideModule" />
第四步:加载显示
if (Build.VERSION.SDK_INT >= 23) { //imageView.setImageResource(R.drawable.ic_gf_default_photo); Glide.with(activity).load(uri).centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL) .error(R.drawable.ic_gf_default_photo) .crossFade() .into(imageView); } else { Glide.with(activity).load(uri).centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL) .placeholder(R.drawable.ic_gf_default_photo) .error(R.drawable.ic_gf_default_photo) .crossFade() .into(imageView); }
Glide加载认证的https图片,okhttp加载
主要重写3个类
1、OkHttpStreamFetcher
public class OkHttpStreamFetcher implements DataFetcher<InputStream>{ private final OkHttpClient client; private final GlideUrl url; private InputStream stream; private ResponseBody responseBody; public OkHttpStreamFetcher(OkHttpClient client, GlideUrl url) { this.client = client; this.url = url; } @Override public InputStream loadData(Priority priority) throws Exception { Request.Builder requestBuilder = new Request.Builder() .url(url.toStringUrl()); for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) { String key = headerEntry.getKey(); requestBuilder.addHeader(key, headerEntry.getValue()); } Request request = requestBuilder.build(); Response response = client.newCall(request).execute(); responseBody = response.body(); if (!response.isSuccessful()) { throw new IOException("Request failed with code: " + response.code()); } long contentLength = responseBody.contentLength(); stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength); return stream; } @Override public void cleanup() { if (stream != null) { try { stream.close(); } catch (IOException e) { // Ignored } } if (responseBody != null) { responseBody.close(); } } @Override public String getId() { return url.getCacheKey(); } @Override public void cancel() { // TODO: call cancel on the client when this method is called on a background thread. See #257 }}2、OkHttpUrlLoader
public class OkHttpUrlLoader implements ModelLoader<GlideUrl, InputStream>{ /** * The default factory for {@link OkHttpUrlLoader}s. */ public static class Factory implements ModelLoaderFactory<GlideUrl, InputStream> { private static volatile OkHttpClient internalClient; private OkHttpClient client; private static OkHttpClient getInternalClient() { if (internalClient == null) { synchronized (Factory.class) { if (internalClient == null) { internalClient = new OkHttpClient(); } } } return internalClient; } /** * Constructor for a new Factory that runs requests using a static singleton client. */ public Factory() { this(getInternalClient()); } /** * Constructor for a new Factory that runs requests using given client. */ public Factory(OkHttpClient client) { this.client = client; } @Override public ModelLoader<GlideUrl, InputStream> build(Context context, GenericLoaderFactory factories) { return new OkHttpUrlLoader(client); } @Override public void teardown() { // Do nothing, this instance doesn't own the client. } } private final OkHttpClient client; public OkHttpUrlLoader(OkHttpClient client) { this.client = client; } @Override public DataFetcher<InputStream> getResourceFetcher(GlideUrl model, int width, int height) { return new OkHttpStreamFetcher(client, model); }}3、最重要的类
public class SimpleGlideModule implements GlideModule{ @Override public void applyOptions(Context context, GlideBuilder builder) { // Do nothing. builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888); } @Override public void registerComponents(Context context, Glide glide) { OkHttpClient.Builder builder = new OkHttpClient.Builder() .sslSocketFactory(overlockCard().getSocketFactory()) .hostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }); glide.register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(builder.build())); } /** * 忽略所有https证书 */ private SSLContext overlockCard() { final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { X509Certificate[] x509Certificates = new X509Certificate[0]; return x509Certificates; } }}; try { SSLContext sslContext = SSLContext.getInstance("SSL"); sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); return sslContext; } catch (Exception e) { Log.e(SimpleGlideModule.class.getSimpleName(), "ssl出现异常"); return null; } }}
新增问题:由于导入第三方module,导致原本好的有不行了。
一般是module中的compile 'com.github.bumptech.glide:okhttp3-integration:1.4.0@aar',注释掉,不然会走原来加载流程
好了,有问题的请加QQ:893151960
阅读全文
0 0
- Glide图片库基本用法及加载认证的https图片
- Glide加载自签名认证的https图片
- Glide图片加载框架的基本用法
- Glide加载https图片
- Glide加载https图片
- Glide加载https图片
- Glide加载https图片
- Android强大的图片加载框架:Glide的基本用法
- Android图片加载框架Glide的基本用法
- Android图片加载框架-Glide的基本用法
- Android图片加载框架 Glide 的基本用法
- Android图片加载框架Glide的基本用法详解
- Glide加载自签名的https图片
- Android图片之Glide(一),Glide的基本用法
- Android中图片加载框架Glide解析1----基本用法
- Android图片加载框架最全解析(一),Glide的基本用法
- (郭霖)Android图片加载框架最全解析(一),Glide的基本用法
- Android图片加载框架最全解析(一),Glide的基本用法
- [ELK] Server 安装
- hive的UDF 函数
- word-break和white-space和text-overflow
- luncene全文搜索引擎(中文分词)
- Bootstrap与angularjs的模态框
- Glide图片库基本用法及加载认证的https图片
- Mysql字符集和校验规则
- HDU 2016
- mq选型的一些网址
- 文章标题
- 设计模式-抽象工厂模式
- spring boot与fastdfs的整合使用
- bzoj2186: [Sdoi2008]沙拉公主的困惑 逆元
- 使用shiro进行登录权限分配