最近做Demo比较多,之前在搭网络框架的时候遇到了图片加载的问题,因为我以前的框架中加载网络图片是没有问题,这次居然出问题,但是其实也不难解决吧。
报错有两个
我的用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'
既然是加载网络图片,那就肯定要有网络访问权限, 在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标签中使用
新建一个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,在加载图片的时候就会自己用到的
*/
}
通过这几步操作基本解决问题,起码我遇到的问题是解决了。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有