下面的代码片段,用于将文件加载到Bitmap中并将此文件保存在内部目录中(即PNG或JPG格式)
final List<Target> targets = new ArrayList<Target>();
final List<Target> targetsNormal = new ArrayList<Target>();
for (int j = 0; j < defaultTileImage.size(); j++) {
final String slangTiles = defaultTileImage.get(j).getPairName() +
ApplicationConstants.SLANG_TILES;
final String normalTiles = defaultTileImage.get(j).getPairName() +
ApplicationConstants.NORMAL_TILES;
final int k = j;
Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Log.i("Targets", "Loaded: " + k);
targets.remove(this);
saveIntoBitmap(bitmap, slangTiles);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
targets.remove(this);
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
Log.i("Targets", "Preparing: " + k);
}
};
Target targetNormal = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Log.i("TargetsNormal", "Loaded: " + k);
targetsNormal.remove(this);
saveIntoBitmapSlang(bitmap, normalTiles);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
targetsNormal.remove(this);
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
Log.i("TargetsNormal", "Preparing: " + k);
}
};
targetsNormal.add(targetNormal);
Picasso.with(MainActivity.this)
.load(defaultTileImage.get(j).getNormalTileImg()) // Start loading the current target
.resize(100, 100)
.into(targetNormal);
targets.add(target);
Picasso.with(MainActivity.this)
.load(defaultTileImage.get(j).getSlangTileImg()) // Start loading the current target
.resize(100, 100)
.into(target);
}
还访问了此link并根据此指南实施,使Target成为强大的参考。但不幸的是,很多时候 onBitmapLoaded 没有得到任何回调。
我非常感谢您帮助解决问题 m
答案 0 :(得分:6)
确保targets
和targetsNormal
是全局变量而不是本地变量。
如果它们是局部变量,只要方法被删除就可以对它们进行垃圾收集。