Cancelling the Image Request Android on the basis of progress - Image loader Volley , Picasso

只愿长相守 提交于 2019-12-07 13:46:58

问题


There are many popular Image loader available in Github for image loading and caching and Managing Image Request.

  1. Universal Image Loader
  2. Volley
  3. Picasso
  4. Fresco (added enhancement )

I came across the requirement if the Imageview if no longer attached to window the request (batch and flight ) will be cancelled .

However the scrolling widget like Listview and Gridview the Imageview is frequently attached and detached from window and so the request in queue are canceled respectively .

My concern is that their no mechanism to trace the progress of image request in all above library .If Imageview is detached the request should be cancelled on the basis of progress , if the byte stream a had already done 75% of work then it should not be cancelled , which in turn will reduce the network bandwidth and decrease network call , as usually user always scroll a lot in Scrolling Widget like Listview and Gridview etc ..

Here is my understanding with Volley :

public boolean removeContainerAndCancelIfNecessary(ImageContainer container) {

    /* logic to check progress of Request */
    mContainers.remove(container);
    if (mContainers.size() == 0) {

        mRequest.cancel();
        return true;
    }
    return false;
}

Here is my understanding with Picasso :

private void cancelExistingRequest(Object target) {

    checkMain();
    Action action = targetToAction.remove(target);
    if (action != null) {

        action.cancel();
        dispatcher.dispatchCancel(action);
    }
    if (target instanceof ImageView) {

        ImageView targetImageView = (ImageView) target;
        DeferredRequestCreator deferredRequestCreator = targetToDeferredRequestCreator.remove(targetImageView);
        if (deferredRequestCreator != null) {

            deferredRequestCreator.cancel();
        }
    }
}

Here is my understanding with Universal Image Loader : I did not figure Universal image loader permits cancelling the inline request .

void cancelDisplayTaskFor(ImageAware imageAware) {

    cacheKeysForImageAwares.remove(imageAware.getId());
} 

Please let me know the possibility to solve it for Network Request only . In case the image is already cached then we will cancel the request .

Battery performance is really important in Android


回答1:


I can't say about Volley and Picasso but I can say how UIL works.

UIL constantly checks ImageView during task processing. If task becomes non-actual (ImageView is recycled (reused) or it's collected by GC) then UIL stops task for this ImageView.

What about if task becomes non-actual while downloading image? UIL aborts downloading if there was downloaded less than 75% of image data. If more than 75% - then UIL completes downloading (i.e. UIL caches image file in disk cache) and then cancels task.



来源:https://stackoverflow.com/questions/27146850/cancelling-the-image-request-android-on-the-basis-of-progress-image-loader-vol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!