关于Picasso加载图片Callback不执行问题
关于Picasso加载图片Callback不执行问题
问题背景
代码大致如下,Target或Callback的回调有时候不执行。
https://github.com/square/picasso/issues/352
第一种:使用Target
Picasso.get().load(URL).into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Log.i(TAG, "onBitmapLoaded: " + from);
}
@Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {
Log.e(TAG, "onBitmapFailed");
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
第二种:使用Callback
ImageView imageView = new ImageView(this);
Picasso.get().load(URL).into(imageView, new Callback() {
@Override
public void onSuccess() {
Log.i(TAG, "onSuccess: ");
}
@Override
public void onError(Exception e) {
Log.e(TAG, "onError" );
}
});
以上代码,通常用在预加载图片的业务中。
问题原因
对于这样偶现的问题,第一反应应该是引用持有问题。果然,上述问题是弱引用问题: WeakReference
。
源码推导:
// 1.先看into方法:RequestCreator.java
public void into(@NonNull Target target) {
// ...
Action action =
new TargetAction(picasso, target, request, memoryPolicy, networkPolicy, errorDrawable,
requestKey, tag, errorResId);
picasso.enqueueAndSubmit(action);
}
// 2.跟到Action:TargetAction.java
final class TargetAction extends Action<Target> {
TargetAction(Picasso picasso, Target target, Request data, int memoryPolicy, int networkPolicy,
Drawable errorDrawable, String key, Object tag, int errorResId) {
// target被传到了super构造器
super(picasso, target, data, memoryPolicy, networkPolicy, errorResId, errorDrawable, key, tag,
false);
}
@Override
void complete(Bitmap result, Picasso.LoadedFrom from) {
if (result == null) {
throw new AssertionError(
String.format("Attempted to complete action with no result!\n%s", this));
}
Target target = getTarget();
if (target != null) { // 这一句判断很关键
target.onBitmapLoaded(result, from);
if (result.isRecycled()) {
throw new IllegalStateException("Target callback must not recycle bitmap!");
}
}
}
@Override
void error(Exception e) {
Target target = getTarget();
if (target != null) { // 这一句判断很关键
if (errorResId != 0) {
target.onBitmapFailed(e, picasso.context.getResources().getDrawable(errorResId));
} else {
target.onBitmapFailed(e, errorDrawable);
}
}
}
}
// 3.往上跟到Action的构造方法:Action.java
Action(Picasso picasso, T target, Request request, int memoryPolicy, int networkPolicy,
int errorResId, Drawable errorDrawable, String key, Object tag, boolean noFade) {
// ...
this.target =
target == null ? null : new RequestWeakReference<>(this, target, picasso.referenceQueue);
// ...
}
有此可见,如果this.target
这个虚引用,要是在gc的时候被回收了,回调自然也不会有了。
场景复现
以下代码模拟系统内存紧张时候的gc过程,这样偶现问题便成了必现,或极大概率出现。
public void preloadImage() {
Picasso.get().load(URL).into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Log.i(TAG, "onBitmapLoaded: " + from);
}
@Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {
Log.e(TAG, "onBitmapFailed");
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
for (int i = 0; i < 1000; i++) {
double[] b = new double[1000000];
}
System.gc();
}
解决方式
可以考虑通过加持引用,不让若引用销毁。方式有很多种,这里可以考虑用匿名内部类来延长变量的生命周期。
第一种:使用Target
public void preloadImage() {
Log.d(TAG, "preloadImage: preloadImage.");
class Ref {
private Target t;
}
final Ref ref = new Ref();
ref.t = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Log.i(TAG, "preloadImage.onBitmapLoaded: " + from + ref.hashCode());
}
@Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {
Log.e(TAG, "preloadImage.onBitmapFailed" + ref.hashCode());
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
@Override
protected void finalize() throws Throwable {
super.finalize();
Log.e(TAG, "target.finalize!");
}
};
Picasso.get().load(URL).into(ref.t);
}
第二种:使用Callback
public void preloadImage() {
Log.d(TAG, "preloadImage: start.");
final ImageView imageView = new ImageView(this);
Picasso.get().load(URL).into(imageView, new Callback() {
@Override
public void onSuccess() {
Log.i(TAG, "preloadImage.onSuccess: " + imageView.hashCode());
}
@Override
public void onError(Exception e) {
Log.e(TAG, "preloadImage.onError" + imageView.hashCode());
}
@Override
protected void finalize() throws Throwable {
super.finalize();
Log.e(TAG, "callback.finalize!");
}
});
}
以上。
关于Picasso加载图片Callback不执行问题的更多相关文章
- Picasso 加载图片到RelativeLayout之解决方案
Picasso 加载图片到ImageView 或者自己的自定义View都是可以直接调用对应API的,但是用into(0直接也加载到RelatieLayout就不好使了,可以这样来: Picasso.w ...
- (Android图片内存优化)Picasso加载图片 教程。。详细版
Picasso 是 Android 上一个强大的图片下载和缓存库. 示例代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Picasso.with( ...
- 实例演示Android异步加载图片
本文给大家演示异步加载图片的分析过程.让大家了解异步加载图片的好处,以及如何更新UI.首先给出main.xml布局文件:简单来说就是 LinearLayout 布局,其下放了2个TextView和5个 ...
- 实例演示Android异步加载图片(转)
本文给大家演示异步加载图片的分析过程.让大家了解异步加载图片的好处,以及如何更新UI.首先给出main.xml布局文件:简单来说就是 LinearLayout 布局,其下放了2个TextView和5个 ...
- jquery的promise实践--连续加载图片
在javascript设计模式实践之代理模式--图片预加载中用代理模式实现了图片预加载功能. 现在就更进一步,完成一个能够一张一张的连续图片加载的功能. 功能: 1.一张一张加载图片. 2.加载错误, ...
- android 加载图片框架--Glide使用详解
一.简介 Glide,一个被google所推荐的图片加载库,作者是bumptech.这个库被广泛运用在google的开源项目中,包括2014年的google I/O大会上发布的官方app.(PS:众所 ...
- JQuery实现无刷新下拉加载图片
最近做的一个项目需要做页面无刷新下拉加载图片,调研了一番,大多都采用检测滚动条达到底部,然后利用ajax加载下一页数据对页面数据进行添加,根据这一逻辑,自己写了一个,具体代码如下: JQu ...
- android 网络加载图片,对图片资源进行优化,并且实现内存双缓存 + 磁盘缓存
经常会用到 网络文件 比如查看大图片数据 资源优化的问题,当然用开源的项目 Android-Universal-Image-Loader 或者 ignition 都是个很好的选择. 在这里把原来 ...
- ListView异步加载图片,完美实现图文混排
昨天参加一个面试,面试官让当场写一个类似于新闻列表的页面,文本数据和图片都从网络上获取,想起我还没写过ListView异步加载图片并实现图文混排效果的文章,so,今天就来写一下,介绍一下经验. Lis ...
随机推荐
- 求1到n的质数个数和O(n)
也许更好的阅读体验 \(\mathcal{AIM}\) 我们知道: 对于一个合数\(x\) 有\(x=p^{a_1}_1*p^{a_2}_2*...*p^{a_n}_n\) 现在给出一个\(n\) 求 ...
- Flume框架的学习使用
Flume框架的学习使用 Flume简介 Flume提供一个分布式的,可靠的,对大数据量的日志进行高效收集.聚集.移动的服务. Flume基于流失架构,容错性强,也很灵活简单 Flume,kafka用 ...
- iframe插入视频自动播放代码
<iframe marginwidth=0 marginheight=0 src='http://www.wsview.com/yzplayerAction!play2.action?autoP ...
- C语言编程入门之--第二章编译环境搭建
第二章 编译环境搭建 导读:C语言程序如何工作,首先需要编译链接成可执行文件,然后就可以运行在不同的环境中,这个“环境”的意思就是比如说,电脑,手机,路由器,蓝牙音箱等等智能设备中,其中编译器启到了关 ...
- WebGL中深度碰撞方法总结
z-fighting问题是三维渲染中常见的问题,本文根据实际工作中遇到的一些场景,进行了系统的总结 一个实际工作中的问题 当两个面离得太近就会发生深度碰撞问题,比如: 遇到深度检测问题,最重要的是先搞 ...
- linux初学者-网络管理篇
linux初学者-网络管理篇 linux学习中,网络管理是非常重要的一个内容,本篇将会介绍一些ip.网关.DNS配置的一些基本内容. 1.ip配置 1.1.ip查询 在linux系统中一般可以使用& ...
- Linux基本操作及安装(部分)
1.分别用cat \tac\nl三个命令查看文件/etc/ssh/sshd_config文件中的内容, 并用自己的话总计出这三个文档操作命令的不同之处? [root@localhost ~]# c ...
- hdoj 4706 Children's Day
题目意思就是用a-z组成一个N,然后到z后又跳回a,输出宽从3到10的N. #include <stdio.h> #include <string.h> char s[14][ ...
- jenkins未授权访问漏洞
jenkins未授权访问漏洞 一.漏洞描述 未授权访问管理控制台,可以通过脚本命令行执行系统命令.通过该漏洞,可以后台管理服务,通过脚本命令行功能执行系统命令,如反弹shell,wget写webshe ...
- SpringMVC学习笔记之---简单入门
SpringMVC简单入门 (一)什么是MVC设计模式 (1)model:模型数据,业务逻辑 (3)view:呈现模型,与用户进行交互 (3)controller:负责接收并处理请求,响应客户端 (二 ...