2

So I'm downloading two images using Picasso at the same time, as follows:

 Picasso.with(this).load(Constants.SERVER_HOME_PAGE_TOP_IMAGE_URL).into(new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            sLastBitmapLoadedDate = Calendar.getInstance();

            int h= (int) (Constants.PAGE_TOP_IMAGE_HEIGHT * densityMultiplier);
            int w= (int) (h * bitmap.getWidth()/((double) bitmap.getHeight()));
            sTipBitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);

            Log.v("Image: ", "one");

            topImage.setImageBitmap(sTipBitmap);
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
        }
    });

    Picasso.with(this).load(Constants.SERVER_HOME_PAGE_BOTTOM_IMAGE_URL).into(new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            sLastBitmapLoadedDate = Calendar.getInstance();

            int h= (int) (Constants.PAGE_BOTTOM_IMAGE_HEIGHT * densityMultiplier);
            int w= (int) (h * bitmap.getWidth()/((double) bitmap.getHeight()));
            sAdBitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);

            Log.v("Image: ", "two");

            bottomImage.setImageBitmap(sAdBitmap);
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
        }
    });

I'm modifying the image size by using Target. So I never hit the onBitMapLoaded on both cases. If i comment out one of the requests, then it will load the image and hit the onBitMapLoaded, where i resize the image.

So I'm for sure going about this the wrong way. If someone could help me out and explain what I am doing wrong.

Thanks!

huey77
  • 643
  • 1
  • 9
  • 24

1 Answers1

0

I've been there. Tried over 3 days to download multiple images using Picasso. However I found that the best way to download multiple images is to use a file downloader library. The FileDownloader library is an incredible choice as it allows to download multiple images in a queue both synchronously and asynchronously.

As a snapshot, this is how you'd download multiple images as a queue using this library:

    final FileDownloadListener queueTarget = new FileDownloadListener() {
            @Override
            protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
            }

            @Override
            protected void connected(BaseDownloadTask task, String etag, boolean isContinue, int soFarBytes, int totalBytes) {
            }

            @Override
            protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
            }

            @Override
            protected void blockComplete(BaseDownloadTask task) {
            }

            @Override
            protected void retry(final BaseDownloadTask task, final Throwable ex, final int retryingTimes, final int soFarBytes) {
            }

            @Override
            protected void completed(BaseDownloadTask task) {
            }

            @Override
            protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {
            }

            @Override
            protected void error(BaseDownloadTask task, Throwable e) {
            }

            @Override
            protected void warn(BaseDownloadTask task) {
            }
        };


final FileDownloadQueueSet queueSet = new FileDownloadQueueSet(downloadListener);

final List<BaseDownloadTask> tasks = new ArrayList<>();
for (int i = 0; i < count; i++) {
     tasks.add(FileDownloader.getImpl().create(Constant.URLS[i]).setTag(i + 1));
}

queueSet.disableCallbackProgressTimes(); // Do not need for each task callback `FileDownloadListener#progress`,
// We just consider which task will complete. so in this way reduce ipc will be effective optimization.

// Each task will auto retry 1 time if download fail.
queueSet.setAutoRetryTimes(1);

if (serial) {
     // Start downloading in serial order.
     queueSet.downloadSequentially(tasks);
     // If your tasks are not a list, invoke such following will more readable:
//      queueSet.downloadSequentially(
//              FileDownloader.getImpl().create(url).setPath(...),
//              FileDownloader.getImpl().create(url).addHeader(...,...),
//              FileDownloader.getImpl().create(url).setPath(...)
//      );
}

if (parallel) {
   // Start parallel download.
   queueSet.downloadTogether(tasks);
   // If your tasks are not a list, invoke such following will more readable:
//    queueSet.downloadTogether(
//            FileDownloader.getImpl().create(url).setPath(...),
//            FileDownloader.getImpl().create(url).setPath(...),
//            FileDownloader.getImpl().create(url).setSyncCallback(true)
//    );
}


queueSet.start();

The entire usage guide is available here : Wiki

Also, This is the question I've had asked in order to download multiple images using Picasso : Downloading images using Picasso creates incorrect images in cache, possible fix?

Community
  • 1
  • 1
OBX
  • 6,044
  • 7
  • 33
  • 77
  • Thanks for the suggestion! I figured out another way. I'm going to use the callback to resize the image there. I'm grabbing the drawable from the imageview and then converting to a bitmap and then resizing. Not the best way, but its only for these two images. – huey77 Jan 31 '17 at 20:24