# Class ImageDownloadService

java.lang.Object
com.codename1.io.ConnectionRequest
com.codename1.io.services.ImageDownloadService

All Implemented Interfaces:
  `IOProgressListener`

public class ImageDownloadService extends ConnectionRequest
Simplifies the process of implementing an image link for labels and lists by binding a request to a component. On the completion of the request a Codename One encoded image is created and installs itself into the given component. For the case of a Label this is seamless, in case of a List renderer the model or the renderer must register itself as a listener and update the data when the response arrives.

- ## Nested Class Summary

  ### Nested classes/interfaces inherited from class ConnectionRequest

  `ConnectionRequest.CachingMode, ConnectionRequest.SSLCertificate`

- ## Field Summary

  ### Fields inherited from class ConnectionRequest

  `PRIORITY_CRITICAL, PRIORITY_HIGH, PRIORITY_LOW, PRIORITY_NORMAL, PRIORITY_REDUNDANT`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ImageDownloadService(String url, Component targetList, int targetOffset, String targetKey)`
  Constructs an image request that will automatically populate the given list when the response arrives.
  `ImageDownloadService(String url, ActionListener l)`
  Accepts the url to bind to the list renderer, on completion the action listener will be invoked with the image so a list can be updated with the data
  `ImageDownloadService(String url, Label parentLabel)`
  Accepts the url to bind to the label, on completion the label will be updated and revalidated with the new image.
  `ImageDownloadService(String url, List targetList, int targetOffset, String targetKey)`
  Constructs an image request that will automatically populate the given list when the response arrives.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static void`
  `addErrorListener(ActionListener listener)`
  Binds an error listener that will fire an instance of NetworkEvent with an error for a specific fetch operation if applicable
  `static void`
  `createImageToFileSystem(String url, Component targetList, int targetOffset, String targetKey, String destFile, Dimension toScale, byte priority)`
  Constructs an image request that will automatically populate the given list when the response arrives, it will cache the file locally as a file in the file storage.
  `static void`
  `createImageToFileSystem(String url, Component targetList, int targetOffset, String targetKey, String destFile, Image placeholder, byte priority)`
  Constructs an image request that will automatically populate the given list when the response arrives, it will cache the file locally as a file in the file storage.
  `static void`
  `createImageToFileSystem(String url, Component targetList, ListModel model, int targetOffset, String targetKey, String destFile, Image placeholder, byte priority)`
  Constructs an image request that will automatically populate the given list when the response arrives, it will cache the file locally as a file in the file storage.
  `static void`
  `createImageToFileSystem(String url, ActionListener callback, String destFile)`
  Constructs an image request that will automatically populate the given Label when the response arrives, it will cache the file locally.
  `static void`
  `createImageToFileSystem(String url, List targetList, int targetOffset, String targetKey, String destFile, Dimension toScale)`
  Constructs an image request that will automatically populate the given list when the response arrives, it will cache the file locally as a file in the file storage.
  `static void`
  `createImageToStorage(String url, Component targetList, int targetOffset, String targetKey, String cacheId, Dimension scale, byte priority)`
  Constructs an image request that will automatically populate the given list when the response arrives, it will cache the file locally as a file in the file storage.
  `static void`
  `createImageToStorage(String url, Component targetList, int targetOffset, String targetKey, String cacheId, Image placeholderImage, byte priority)`
  Constructs an image request that will automatically populate the given list when the response arrives, it will cache the file locally as a file in the file storage.
  `static void`
  `createImageToStorage(String url, Component targetList, ListModel model, int targetOffset, String targetKey, String cacheId, Image placeholderImage, byte priority)`
  Constructs an image request that will automatically populate the given list when the response arrives, it will cache the file locally as a file in the file storage.
  `static void`
  `createImageToStorage(String url, ActionListener callback, String cacheId)`
  `static void`
  `createImageToStorage(String url, ActionListener callback, String cacheId, boolean keep)`
  Constructs an image request that will automatically populate the given Label when the response arrives, it will cache the file locally.
  `static void`
  `createImageToStorage(String url, Label l, String cacheId, Dimension toScale)`
  Constructs an image request that will automatically populate the given Label when the response arrives, it will cache the file locally to the Storage
  `static void`
  `createImageToStorage(String url, Label l, String cacheId, Dimension toScale, byte priority)`
  Constructs an image request that will automatically populate the given Label when the response arrives, it will cache the file locally to the Storage
  `static void`
  `createImageToStorage(String url, Label l, String cacheId, Image placeholder, byte priority)`
  Constructs an image request that will automatically populate the given Label when the response arrives, it will cache the file locally to the Storage
  `static void`
  `createImageToStorage(String url, List targetList, int targetOffset, String targetKey, String cacheId, Dimension scale)`
  Constructs an image request that will automatically populate the given list when the response arrives, it will cache the file locally as a file in the file storage.
  `boolean`
  `equals(Object o)`
  `static int`
  `getDefaultTimeout()`
  Indicates the default timeout to use for image download service, -1 indicates no default is set.
  `EncodedImage`
  `getResult()`
  Returns the image returned from the server, this method is useful for renderers
  `protected void`
  `handleErrorResponseCode(int code, String message)`
  Handles a server response code that is not 200 and not a redirect (unless redirect handling is disabled)
  `protected void`
  `handleException(Exception err)`
  Handles an exception thrown when performing a network operation, the default implementation shows a retry dialog.
  `int`
  `hashCode()`
  `static boolean`
  `isAlwaysRevalidate()`
  By default lists don't revalidate on every change to avoid "jumpiness" when scrolling
  `static boolean`
  `isDefaultMaintainAspectRatio()`

  ###### Returns

  `boolean`
  `isDownloadToStyles()`
  Downloads the image to the style objects associated with this component, effectively sets the bgImage property on all the styles for the component instead of invoking setIcon
  `static boolean`
  `isFastScale()`
  Fast scaling uses runtime draw scaling rather than the Image scaled method.
  `boolean`
  `isMaintainAspectRatio()`

  ###### Returns

  `protected void`
  `postResponse()`
  A callback method that's invoked on the EDT after the readResponse() method has finished, this is the place where developers should change their Codename One user interface to avoid race conditions that might be triggered by modifications within readResponse.
  `protected void`
  `readResponse(InputStream input)`
  Callback for the server response with the input stream from the server.
  `static void`
  `removeErrorListener(ActionListener listener)`
  Unbinds an error listener that will fire an instance of NetworkEvent with an error for a specific fetch operation if applicable
  `static void`
  `setAlwaysRevalidate(boolean aAlwaysRevalidate)`
  By default lists don't revalidate on every change to avoid "jumpiness" when scrolling
  `static void`
  `setDefaultMaintainAspectRatio(boolean aDefaultMaintainAspectRatio)`

  ###### Parameters

  `static void`
  `setDefaultTimeout(int aDefaultTimeout)`
  Indicates the default timeout to use for image download service, -1 indicates no default is set.
  `void`
  `setDownloadToStyles(boolean downloadToStyles)`
  Downloads the image to the style objects associated with this component, effectively sets the bgImage property on all the styles for the component instead of invoking setIcon
  `protected void`
  `setEntryInListModel(int offset, Image img)`
  This method is invoked when an image finished downloading and should be set to an offset in the list model.
  `static void`
  `setFastScale(boolean aFastScale)`
  Fast scaling uses runtime draw scaling rather than the Image scaled method.
  `void`
  `setMaintainAspectRatio(boolean maintainAspectRatio)`

  ###### Parameters

  ### Methods inherited from class ConnectionRequest

  `addArgument, addArgument, addArgument, addArgumentArray, addArgumentNoEncoding, addArgumentNoEncoding, addArgumentNoEncodingArray, addArguments, addExceptionListener, addRequestHeader, addResponseCodeListener, addResponseListener, buildRequestBody, cacheUnmodified, canGetSSLCertificates, checkSSLCertificates, cookieReceived, cookieSent, createRequestURL, downloadImageToFileSystem, downloadImageToFileSystem, downloadImageToFileSystem, downloadImageToFileSystem, downloadImageToFileSystem, downloadImageToFileSystem, downloadImageToStorage, downloadImageToStorage, downloadImageToStorage, downloadImageToStorage, downloadImageToStorage, downloadImageToStorage, fetchJSON, fetchJSONAsync, fireResponseListener, getCachedData, getCacheMode, getContentLength, getContentType, getCookieHeader, getDefaultCacheMode, getDefaultUserAgent, getDestinationFile, getDestinationStorage, getDisposeOnCompletion, getHeader, getHeaderFieldNames, getHeaders, getHttpMethod, getPriority, getReadTimeout, getRequestBody, getRequestBodyData, getResponseCode, getResponseContentType, getResponseData, getResponseErrorMessage, getResposeCode, getShowOnInit, getSilentRetryCount, getSSLCertificates, getTimeout, getUrl, getUserAgent, getYield, handleIOException, handleRuntimeException, hasResponseListeners, initConnection, initCookieHeader, ioStreamUpdate, isCheckSSLCertificates, isCookiesEnabled, isCookiesEnabledDefault, isDefaultFollowRedirects, isDuplicateSupported, isFailSilently, isFollowRedirects, isHandleErrorCodesInGlobalErrorHandler, isInsecure, isKilled, isNativeCookieSharingSupported, isPausable, isPaused, isPost, isReadRequest, isReadResponseForErrors, isReadResponseForErrorsDefault, isReadTimeoutSupported, isRedirecting, isWriteRequest, kill, onRedirect, pause, purgeCache, purgeCacheDirectory, readErrorCodeHeaders, readHeaders, removeAllArguments, removeArgument, removeExceptionListener, removeResponseCodeListener, removeResponseListener, resume, retry, setCacheMode, setCheckSSLCertificates, setChunkedStreamingMode, setContentType, setCookieHeader, setCookiesEnabled, setCookiesEnabledDefault, setDefaultCacheMode, setDefaultFollowRedirects, setDefaultUserAgent, setDestinationFile, setDestinationStorage, setDisposeOnCompletion, setDuplicateSupported, setFailSilently, setFollowRedirects, setHandleErrorCodesInGlobalErrorHandler, setHttpMethod, setInsecure, setKilled, setPaused, setPost, setPriority, setReadRequest, setReadResponseForErrors, setReadResponseForErrorsDefault, setReadTimeout, setRequestBody, setRequestBody, setShowOnInit, setSilentRetryCount, setTimeout, setUrl, setUseNativeCookieStore, setUserAgent, setWriteRequest, shouldAutoCloseResponse, shouldConvertPostToGetOnRedirect, shouldStop, shouldWriteUTFAsGetBytes, validate`

  ### Methods inherited from class Object

  `clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### ImageDownloadService

    public ImageDownloadService(String url, ActionListener l)

    Accepts the url to bind to the list renderer, on completion the action listener will be invoked with the image so a list can be updated with the data

    ###### Parameters

    - `url`: the image URL

    - `l`: an action listener callback

  - ### ImageDownloadService

    public ImageDownloadService(String url, Component targetList, int targetOffset, String targetKey)

    Constructs an image request that will automatically populate the given list when the response arrives. This assumes the GenericListCellRenderer style of list which relies on a map based model approach.

    ###### Parameters

    - `url`: the image URL

    - `targetList`: the list that should be updated when the data arrives

    - `targetOffset`: the offset within the list to insert the image

    - `targetKey`: the key for the map in the target offset

  - ### ImageDownloadService

    public ImageDownloadService(String url, List targetList, int targetOffset, String targetKey)

    Constructs an image request that will automatically populate the given list when the response arrives. This assumes the GenericListCellRenderer style of list which relies on a Map based model approach.

    ###### Parameters

    - `url`: the image URL

    - `targetList`: the list that should be updated when the data arrives

    - `targetOffset`: the offset within the list to insert the image

    - `targetKey`: the key for the map in the target offset

  - ### ImageDownloadService

    public ImageDownloadService(String url, Label parentLabel)

    Accepts the url to bind to the label, on completion the label will be updated and revalidated with the new image.

    ###### Parameters

    - `url`: the image URL

    - `parentLabel`: the label to update

- ## Method Details

  - ### isFastScale

    public static boolean isFastScale()

    Fast scaling uses runtime draw scaling rather than the Image scaled method. This works better on smartphones but doesn't work well on feature phones

    ###### Returns

    the fastScale

  - ### setFastScale

    public static void setFastScale(boolean aFastScale)

    Fast scaling uses runtime draw scaling rather than the Image scaled method. This works better on smartphones but doesn't work well on feature phones

    ###### Parameters

    - `aFastScale`: the fastScale to set

  - ### isAlwaysRevalidate

    public static boolean isAlwaysRevalidate()

    By default lists don't revalidate on every change to avoid "jumpiness" when scrolling

    ###### Returns

    the alwaysRevalidate

  - ### setAlwaysRevalidate

    public static void setAlwaysRevalidate(boolean aAlwaysRevalidate)

    By default lists don't revalidate on every change to avoid "jumpiness" when scrolling

    ###### Parameters

    - `aAlwaysRevalidate`: the alwaysRevalidate to set

  - ### isDefaultMaintainAspectRatio

    public static boolean isDefaultMaintainAspectRatio()

    ###### Returns

    the defaultMaintainAspectRatio

  - ### setDefaultMaintainAspectRatio

    public static void setDefaultMaintainAspectRatio(boolean aDefaultMaintainAspectRatio)

    ###### Parameters

    - `aDefaultMaintainAspectRatio`: the defaultMaintainAspectRatio to set

  - ### getDefaultTimeout

    public static int getDefaultTimeout()

    Indicates the default timeout to use for image download service, -1 indicates no default is set.

    ###### Returns

    the defaultTimeout

  - ### setDefaultTimeout

    public static void setDefaultTimeout(int aDefaultTimeout)

    Indicates the default timeout to use for image download service, -1 indicates no default is set.

    ###### Parameters

    - `aDefaultTimeout`: the defaultTimeout to set

  - ### createImageToFileSystem

    public static void createImageToFileSystem(String url, List targetList, int targetOffset, String targetKey, String destFile, Dimension toScale)

    Constructs an image request that will automatically populate the given list when the response arrives, it will cache the file locally as a file in the file storage. This assumes the GenericListCellRenderer style of list which relies on a map based model approach.

    ###### Parameters

    - `url`: the image URL

    - `targetList`: the list that should be updated when the data arrives

    - `targetOffset`: the offset within the list to insert the image

    - `targetKey`: the key for the map in the target offset

    - `destFile`: local file to store the data into the given path

  - ### createImageToFileSystem

    public static void createImageToFileSystem(String url, Component targetList, int targetOffset, String targetKey, String destFile, Dimension toScale, byte priority)

    Constructs an image request that will automatically populate the given list when the response arrives, it will cache the file locally as a file in the file storage. This assumes the GenericListCellRenderer style of list which relies on a map based model approach.

    ###### Parameters

    - `url`: the image URL

    - `targetList`: the list that should be updated when the data arrives

    - `targetOffset`: the offset within the list to insert the image

    - `targetKey`: the key for the map in the target offset

    - `destFile`: local file to store the data into the given path

  - ### createImageToFileSystem

    public static void createImageToFileSystem(String url, Component targetList, int targetOffset, String targetKey, String destFile, Image placeholder, byte priority)

    Constructs an image request that will automatically populate the given list when the response arrives, it will cache the file locally as a file in the file storage. This assumes the GenericListCellRenderer style of list which relies on a map based model approach.

    ###### Parameters

    - `url`: the image URL

    - `targetList`: the list that should be updated when the data arrives

    - `targetOffset`: the offset within the list to insert the image

    - `targetKey`: the key for the map in the target offset

    - `destFile`: local file to store the data into the given path

  - ### createImageToFileSystem

    public static void createImageToFileSystem(String url, Component targetList, ListModel model, int targetOffset, String targetKey, String destFile, Image placeholder, byte priority)

    Constructs an image request that will automatically populate the given list when the response arrives, it will cache the file locally as a file in the file storage. This assumes the GenericListCellRenderer style of list which relies on a map based model approach.

    ###### Parameters

    - `url`: the image URL

    - `targetList`: the list that should be updated when the data arrives

    - `model`: the list model

    - `targetOffset`: the offset within the list to insert the image

    - `targetKey`: the key for the map in the target offset

    - `destFile`: local file to store the data into the given path

  - ### addErrorListener

    public static void addErrorListener(ActionListener listener)

    Binds an error listener that will fire an instance of NetworkEvent with an error for a specific fetch operation if applicable

    ###### Parameters

    - `listener`: the listener

  - ### removeErrorListener

    public static void removeErrorListener(ActionListener listener)

    Unbinds an error listener that will fire an instance of NetworkEvent with an error for a specific fetch operation if applicable

    ###### Parameters

    - `listener`: the listener

  - ### createImageToStorage

    public static void createImageToStorage(String url, List targetList, int targetOffset, String targetKey, String cacheId, Dimension scale)

    Constructs an image request that will automatically populate the given list when the response arrives, it will cache the file locally as a file in the file storage. This assumes the GenericListCellRenderer style of list which relies on a map based model approach.

    ###### Parameters

    - `url`: the image URL

    - `targetList`: the list that should be updated when the data arrives

    - `targetOffset`: the offset within the list to insert the image

    - `targetKey`: the key for the map in the target offset

    - `cacheId`: a unique identifier to be used to store the image into storage

    - `scale`: the scale of the image to put in the List or null

  - ### createImageToStorage

    public static void createImageToStorage(String url, Component targetList, int targetOffset, String targetKey, String cacheId, Dimension scale, byte priority)

    Constructs an image request that will automatically populate the given list when the response arrives, it will cache the file locally as a file in the file storage. This assumes the GenericListCellRenderer style of list which relies on a map based model approach.

    ###### Parameters

    - `url`: the image URL

    - `targetList`: the list that should be updated when the data arrives

    - `targetOffset`: the offset within the list to insert the image

    - `targetKey`: the key for the map in the target offset

    - `cacheId`: a unique identifier to be used to store the image into storage

    - `scale`: the scale of the image to put in the List or null

  - ### createImageToStorage

    public static void createImageToStorage(String url, Component targetList, int targetOffset, String targetKey, String cacheId, Image placeholderImage, byte priority)

    Constructs an image request that will automatically populate the given list when the response arrives, it will cache the file locally as a file in the file storage. This assumes the GenericListCellRenderer style of list which relies on a map based model approach.

    ###### Parameters

    - `url`: the image URL

    - `targetList`: the list that should be updated when the data arrives

    - `targetOffset`: the offset within the list to insert the image

    - `targetKey`: the key for the map in the target offset

    - `cacheId`: a unique identifier to be used to store the image into storage

    - `placeholderImage`: the image placeholder

  - ### createImageToStorage

    public static void createImageToStorage(String url, Component targetList, ListModel model, int targetOffset, String targetKey, String cacheId, Image placeholderImage, byte priority)

    Constructs an image request that will automatically populate the given list when the response arrives, it will cache the file locally as a file in the file storage. This assumes the GenericListCellRenderer style of list which relies on a map based model approach.

    ###### Parameters

    - `url`: the image URL

    - `targetList`: the list that should be updated when the data arrives

    - `model`: the model destination

    - `targetOffset`: the offset within the list to insert the image

    - `targetKey`: the key for the map in the target offset

    - `cacheId`: a unique identifier to be used to store the image into storage

    - `placeholderImage`: the image placeholder

  - ### createImageToStorage

    public static void createImageToStorage(String url, Label l, String cacheId, Dimension toScale)

    Constructs an image request that will automatically populate the given Label when the response arrives, it will cache the file locally to the Storage

    ###### Parameters

    - `url`: the image URL

    - `l`: @param l the Label that should be updated when the data arrives to just use storage and the url as the key

    - `cacheId`: a unique identifier to be used to store the image into storage

    - `toScale`: the scale dimension or null

  - ### createImageToStorage

    public static void createImageToStorage(String url, Label l, String cacheId, Dimension toScale, byte priority)

    Constructs an image request that will automatically populate the given Label when the response arrives, it will cache the file locally to the Storage

    ###### Parameters

    - `url`: the image URL

    - `l`: @param l the Label that should be updated when the data arrives to just use storage and the url as the key

    - `cacheId`: a unique identifier to be used to store the image into storage

    - `toScale`: the scale dimension or null

    - `priority`: the priority for the task

  - ### createImageToStorage

    public static void createImageToStorage(String url, Label l, String cacheId, Image placeholder, byte priority)

    Constructs an image request that will automatically populate the given Label when the response arrives, it will cache the file locally to the Storage

    ###### Parameters

    - `url`: the image URL

    - `l`: @param l the Label that should be updated when the data arrives to just use storage and the url as the key

    - `cacheId`: a unique identifier to be used to store the image into storage

    - `placeholder`: the image that will appear as a placeholder

    - `priority`: the priority for the task

  - ### createImageToFileSystem

    public static void createImageToFileSystem(String url, ActionListener callback, String destFile)

    Constructs an image request that will automatically populate the given Label when the response arrives, it will cache the file locally.

    ###### Parameters

    - `url`: the image URL

    - `callback`: the callback that should be updated when the data arrives

    - `destFile`: local file to store the data into the given path

  - ### createImageToStorage

    public static void createImageToStorage(String url, ActionListener callback, String cacheId)

  - ### createImageToStorage

    public static void createImageToStorage(String url, ActionListener callback, String cacheId, boolean keep)

    Constructs an image request that will automatically populate the given Label when the response arrives, it will cache the file locally.

    ###### Parameters

    - `url`: the image URL

    - `callback`: the callback that should be updated when the data arrives

    - `cacheId`: a unique identifier to be used to store the image into storage

    - `keep`: if set to true keeps the file in RAM once loaded

  - ### setEntryInListModel

    protected void setEntryInListModel(int offset, Image img)

    This method is invoked when an image finished downloading and should be set to an offset in the list model. This is useful for special cases with complex list model hierarchies or proxies.

    ###### Parameters

    - `offset`: the offset in the list given when creating the service

    - `img`: the image

  - ### handleException

    protected void handleException(Exception err)

    Handles an exception thrown when performing a network operation, the default implementation shows a retry dialog.

    ###### Parameters

    - `err`: the exception thrown
    Overrides:
      `handleException` in class `ConnectionRequest`

  - ### handleErrorResponseCode

    protected void handleErrorResponseCode(int code, String message)

    Handles a server response code that is not 200 and not a redirect (unless redirect handling is disabled)

    ###### Parameters

    - `code`: the response code from the server

    - `message`: the response message from the server

    Overrides:
      `handleErrorResponseCode` in class `ConnectionRequest`

  - ### postResponse

    protected void postResponse()
    A callback method that's invoked on the EDT after the readResponse() method has finished, this is the place where developers should change their Codename One user interface to avoid race conditions that might be triggered by modifications within readResponse. Notice this method is only invoked on a successful response and will not be invoked in case of a failure.
    Overrides:
      `postResponse` in class `ConnectionRequest`

  - ### readResponse

    protected void readResponse(InputStream input) throws IOException

    Callback for the server response with the input stream from the server. This method is invoked on the network thread

    ###### Parameters

    - `input`: the input stream containing the response

    ###### Throws

    - `IOException`: when a read input occurs
    Overrides:
      `readResponse` in class `ConnectionRequest`
    Throws:
      `IOException`

  - ### getResult

    public EncodedImage getResult()

    Returns the image returned from the server, this method is useful for renderers

    ###### Returns

    the result

  - ### isDownloadToStyles

    public boolean isDownloadToStyles()

    Downloads the image to the style objects associated with this component, effectively sets the bgImage property on all the styles for the component instead of invoking setIcon

    ###### Returns

    the downloadToStyles

  - ### setDownloadToStyles

    public void setDownloadToStyles(boolean downloadToStyles)

    Downloads the image to the style objects associated with this component, effectively sets the bgImage property on all the styles for the component instead of invoking setIcon

    ###### Parameters

    - `downloadToStyles`: the downloadToStyles to set

  - ### equals

    public boolean equals(Object o)
    Overrides:
      `equals` in class `ConnectionRequest`

  - ### hashCode

    public int hashCode()
    Overrides:
      `hashCode` in class `ConnectionRequest`

  - ### isMaintainAspectRatio

    public boolean isMaintainAspectRatio()

    ###### Returns

    the maintainAspectRatio

  - ### setMaintainAspectRatio

    public void setMaintainAspectRatio(boolean maintainAspectRatio)

    ###### Parameters

    - `maintainAspectRatio`: the maintainAspectRatio to set