前言
最近做Demo比较多,之前在搭网络框架的时候遇到了图片加载的问题,因为我以前的框架中加载网络图片是没有问题,这次居然出问题,但是其实也不难解决吧。
异常问题
报错有两个
① Failed to find GeneratedAppGlideModule. You should include an annotationProcessor compile dependency on com.github.bumptech.glide:compiler in your application and a @GlideModule annotated AppGlideModule implementation or LibraryGlideModules will be silently ignored
② Glide: Load failed for for http://cn.bing.com/th?id=OHR.StStephens_ZH-CN9373191410_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp with size [1440x2708] class com.bumptech.glide.load.engine.GlideException: Failed to load resource
我的用Glide版本是4.11.0
解决问题
① 确认依赖的库没有问题
在工程的build.gradle下
repositories {
google()
jcenter()
mavenCentral()//新增
}
然后是模块的build.gradle下的dependencies中添加
//图片加载框架
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
② 网络权限与Android版本
既然是加载网络图片,那就肯定要有网络访问权限,
在AndroidManifest.xml中增加
<uses-permission android:name="android.permission.INTERNET" />
然后就是确认你的Android终端的版本,不管你是用模拟器还是使用真机,都会有Android对应的版本,而Android9.0包括之后默认的网络访问方式是https的,如果你的图片地址是https的那还好,如果是http的那就要配置一下了,在res下新建一个xml文件夹,然后在xml文件夹下新建network_security_config.xml,如下图所示
network_security_config.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
然后在AndroidManifest.xml里面的application标签中使用
③ 继承AppGlideModule
新建一个MyAppGlideModule类,继承AppGlideModule,并增加@GlideModule注解,即可,当Glide加载图片时会自动调用这个类,你不用管它。
代码如下:
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.AppGlideModule;
@GlideModule
public class MyAppGlideModule extends AppGlideModule {
/**
* Failed to find GeneratedAppGlideModule.
* You should include an annotationProcessor
* compile dependency on com.github.bumptech.glide:compiler
* in your application and a @GlideModule annotated
* AppGlideModule implementation or LibraryGlideModules will be silently ignored.
* 为了解决这个异常提示特意新建了一个工具类,只要继承了AppGlideModule,在加载图片的时候就会自己用到的
*/
}
通过这几步操作基本解决问题,起码我遇到的问题是解决了。