Android Glide加载网络图片不显示,但用网页打开又正常显示

网上找了好久实在真的找不到刚开始用的是glide4.8.0的版本,后面又用4.12.0版本刚开始以为是版本问题才改的

就报这样的错误

call GlideException#logRootCauses(String) for more detail

028 21084-21084 I/Glide: Root cause (5 of 8)
    java.lang.RuntimeException: setDataSourceCallback failed: status = 0x80000000
        at android.media.MediaMetadataRetriever._setDataSource(Native Method)
        at android.media.MediaMetadataRetriever.setDataSource(MediaMetadataRetriever.java:211)
        at com.bumptech.glide.load.resource.bitmap.VideoDecoder$ByteBufferInitializer.initialize(VideoDecoder.java:324)
        at com.bumptech.glide.load.resource.bitmap.VideoDecoder$ByteBufferInitializer.initialize(VideoDecoder.java:318)
        at com.bumptech.glide.load.resource.bitmap.VideoDecoder.decode(VideoDecoder.java:173)
        at com.bumptech.glide.load.engine.DecodePath.decodeResourceWithList(DecodePath.java:92)
        at com.bumptech.glide.load.engine.DecodePath.decodeResource(DecodePath.java:70)
        at com.bumptech.glide.load.engine.DecodePath.decode(DecodePath.java:59)
        at com.bumptech.glide.load.engine.LoadPath.loadWithExceptionList(LoadPath.java:76)
        at com.bumptech.glide.load.engine.LoadPath.load(LoadPath.java:57)
        at com.bumptech.glide.load.engine.DecodeJob.runLoadPath(DecodeJob.java:529)
        at com.bumptech.glide.load.engine.DecodeJob.decodeFromFetcher(DecodeJob.java:493)
        at com.bumptech.glide.load.engine.DecodeJob.decodeFromData(DecodeJob.java:479)
        at com.bumptech.glide.load.engine.DecodeJob.decodeFromRetrievedData(DecodeJob.java:430)
        at com.bumptech.glide.load.engine.DecodeJob.onDataFetcherReady(DecodeJob.java:394)
        at com.bumptech.glide.load.engine.SourceGenerator.onDataFetcherReady(SourceGenerator.java:176)
        at com.bumptech.glide.load.engine.DataCacheGenerator.onDataReady(DataCacheGenerator.java:94)
        at com.bumptech.glide.load.model.ByteBufferFileLoader$ByteBuffe

① 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.12.0

解决问题
① 确认依赖的库没有问题
在工程的build.gradle下

​
repositories {
        google()
        jcenter()
        mavenCentral()//新增
    }

​



然后是模块的build.gradle下的dependencies中添加

    

//图片加载框架
    implementation 'com.github.bumptech.glide:glide:4.12.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'



② 网络权限与Android版本
既然是加载网络图片,那就肯定要有网络访问权限,
在AndroidManifest.xml中增加



然后就是确认你的Android终端的版本,不管你是用模拟器还是使用真机,都会有Android对应的版本,而Android9.0包括之后默认的网络访问方式是https的,如果你的图片地址是https的那还好,如果是http的那就要配置一下了,在res下新建一个xml文件夹,然后在xml文件夹下新建network_security_config.xml,如下图所示

network_security_config.xml代码如下:



    



然后在AndroidManifest.xml里面的application标签中使用

        android:networkSecurityConfig="@xml/network_security_config"


③ 继承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,在加载图片的时候就会自己用到的
     */

}



通过这几步操作基本解决问题,我遇到的问题是解决了。

Android Glide加载网络图片不显示,但用网页打开又正常显示_第1张图片

 

你可能感兴趣的:(工具,android,功能实现,android,ide,网络,java)