Picasso Targets onBitmapLoaded not called for loop

Below is the code snippet for uploading the file to Bitmap and save this file in the internal directory (for example, in PNG or JPG format)

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);
}

Also visited this link and implemented in accordance with this guide, which makes Target a strong link. But unfortunately, a lot of time onBitmapLoaded does not receive any callback.

I sincerely appreciate your help in solving the problem.

+3
source share
1 answer

Make sure that targetsu targetsNormalare global variables instead of local ones.

If they are local variables, they can be garbage collected as soon as the method remains.

+5

Source: https://habr.com/ru/post/1614417/


All Articles