如何从Glide记录每个请求网址?

我已经使用Glide下载图像很长一段时间了,所以像这样的代码无处不在:


      GlideApp.with(getContext())

            .load(imgUrl)

            .into(imgView);

突然间,我需要记录每个图像的URL以进行进一步分析。而不是搜索Glade的每个用法并像这样更改代码:


      GlideApp.with(getContext())

            .load(imgUrl)

            .listener(requestListener) // log urls through listener

            .into(imgAd);

有没有办法像在Glide的默认选项中那样全局添加requestListener,这样我就不需要在整个应用程序中搜索和更改每个Glide用法?


我使用Glide v4,并且我已经检查了将URL直接记录到logcat而不是我的本地存储以进行进一步分析的帖子。


湖上湖
浏览 82回答 1
1回答

波斯汪

Glide 4.9.0 可以设置默认请求Listener。链接 https://github.com/bumptech/glide/releases/tag/v4.9.0 https://github.com/bumptech/glide/commit/37127f0f817d4a11dfdcc447946397b5288de593在自定义 AppGlideModule 中@GlideModulepublic class MyAppGlideModule extends AppGlideModule {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {&nbsp; &nbsp; &nbsp; &nbsp; builder.addGlobalRequestListener(new RequestListener<Object>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Object> target, boolean isFirstResource) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public boolean onResourceReady(Object resource, Object model, Target<Object> target, DataSource dataSource, boolean isFirstResource) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}在自定义活动片段中GlideApp.with(object).addDefaultRequestListener()Glide.with(object).addDefaultRequestListener()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java