public class ImageDownloadService

  1. Object
  2. ConnectionRequest
  3. ImageDownloadService

ImplementsIOProgressListener

Deprecated. this class uses an unconventional storage mechanism and has many issues, we recommend replacing it with the simpler and more powerful URLImage or Util.dowloadFileTo* calls.
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.

Constructors

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
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.
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.
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.

Methods

public static boolean isFastScale()Fast scaling uses runtime draw scaling rather than the Image scaled method.
public static void setFastScale(boolean aFastScale)Fast scaling uses runtime draw scaling rather than the Image scaled method.
public static boolean isAlwaysRevalidate()By default lists don’t revalidate on every change to avoid “jumpiness” when scrolling
public static void setAlwaysRevalidate(boolean aAlwaysRevalidate)By default lists don’t revalidate on every change to avoid “jumpiness” when scrolling
public static boolean isDefaultMaintainAspectRatio()
public static void setDefaultMaintainAspectRatio(boolean aDefaultMaintainAspectRatio)
public static int getDefaultTimeout()Indicates the default timeout to use for image download service, -1 indicates no default is set.
public static void setDefaultTimeout(int aDefaultTimeout)Indicates the default timeout to use for image download service, -1 indicates no default is set.
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.
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.
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.
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.
public static synchronized 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
public static synchronized 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
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.
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.
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.
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.
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
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
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
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.
public static void createImageToStorage(String url, ActionListener callback, String cacheId)
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.
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.
protected void handleException(Exception err)Handles an exception thrown when performing a network operation, the default implementation shows a retry dialog.
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 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) throws IOExceptionCallback for the server response with the input stream from the server.
public EncodedImage getResult()Returns the image returned from the server, this method is useful for renderers
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
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
public boolean equals(Object o)Indicates whether some other object is “equal to” this one.
public int hashCode()Returns a hash code value for the object.
public boolean isMaintainAspectRatio()
public void setMaintainAspectRatio(boolean maintainAspectRatio)

Inherited nested types

Inherited fields

Inherited methods

From ConnectionRequest

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

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 String
the image URL
l ActionListener
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 String
the image URL
targetList Component
the list that should be updated when the data arrives
targetOffset int
the offset within the list to insert the image
targetKey String
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 String
the image URL
targetList List
the list that should be updated when the data arrives
targetOffset int
the offset within the list to insert the image
targetKey String
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 String
the image URL
parentLabel Label
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 boolean
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 boolean
the alwaysRevalidate to set

isDefaultMaintainAspectRatio

public static boolean isDefaultMaintainAspectRatio()

Returns

the defaultMaintainAspectRatio

setDefaultMaintainAspectRatio

public static void setDefaultMaintainAspectRatio(boolean aDefaultMaintainAspectRatio)

Parameters

aDefaultMaintainAspectRatio boolean
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 int
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 String
the image URL
targetList List
the list that should be updated when the data arrives
targetOffset int
the offset within the list to insert the image
targetKey String
the key for the map in the target offset
destFile String
local file to store the data into the given path
toScale Dimension
Not documented.

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 String
the image URL
targetList Component
the list that should be updated when the data arrives
targetOffset int
the offset within the list to insert the image
targetKey String
the key for the map in the target offset
destFile String
local file to store the data into the given path
toScale Dimension
Not documented.
priority byte
Not documented.

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 String
the image URL
targetList Component
the list that should be updated when the data arrives
targetOffset int
the offset within the list to insert the image
targetKey String
the key for the map in the target offset
destFile String
local file to store the data into the given path
placeholder Image
Not documented.
priority byte
Not documented.

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 String
the image URL
targetList Component
the list that should be updated when the data arrives
model ListModel
the list model
targetOffset int
the offset within the list to insert the image
targetKey String
the key for the map in the target offset
destFile String
local file to store the data into the given path
placeholder Image
Not documented.
priority byte
Not documented.

addErrorListener

public static synchronized 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 ActionListener
the listener

removeErrorListener

public static synchronized 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 ActionListener
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 String
the image URL
targetList List
the list that should be updated when the data arrives
targetOffset int
the offset within the list to insert the image
targetKey String
the key for the map in the target offset
cacheId String
a unique identifier to be used to store the image into storage
scale Dimension
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 String
the image URL
targetList Component
the list that should be updated when the data arrives
targetOffset int
the offset within the list to insert the image
targetKey String
the key for the map in the target offset
cacheId String
a unique identifier to be used to store the image into storage
scale Dimension
the scale of the image to put in the List or null
priority byte
Not documented.

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 String
the image URL
targetList Component
the list that should be updated when the data arrives
targetOffset int
the offset within the list to insert the image
targetKey String
the key for the map in the target offset
cacheId String
a unique identifier to be used to store the image into storage
placeholderImage Image
the image placeholder
priority byte
Not documented.

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 String
the image URL
targetList Component
the list that should be updated when the data arrives
model ListModel
the model destination
targetOffset int
the offset within the list to insert the image
targetKey String
the key for the map in the target offset
cacheId String
a unique identifier to be used to store the image into storage
placeholderImage Image
the image placeholder
priority byte
Not documented.

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 String
the image URL
l Label
the Label that should be updated when the data arrives to just use storage and the url as the key
cacheId String
a unique identifier to be used to store the image into storage
toScale Dimension
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 String
the image URL
l Label
the Label that should be updated when the data arrives to just use storage and the url as the key
cacheId String
a unique identifier to be used to store the image into storage
toScale Dimension
the scale dimension or null
priority byte
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 String
the image URL
l Label
the Label that should be updated when the data arrives to just use storage and the url as the key
cacheId String
a unique identifier to be used to store the image into storage
placeholder Image
the image that will appear as a placeholder
priority byte
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 String
the image URL
callback ActionListener
the callback that should be updated when the data arrives
destFile String
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 String
the image URL
callback ActionListener
the callback that should be updated when the data arrives
cacheId String
a unique identifier to be used to store the image into storage
keep boolean
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 int
the offset in the list given when creating the service
img Image
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 Exception
the exception thrown

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 int
the response code from the server
message String
the response message from the server

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.

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 InputStream
the input stream containing the response

Throws

IOException
when a read input occurs

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 boolean
the downloadToStyles to set

equals

public boolean equals(Object o)
Indicates whether some other object is “equal to” this one. The equals method implements an equivalence relation: It is reflexive: for any reference value x, x.equals(x) should return true. It is symmetric: for any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true. It is transitive: for any reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true. It is consistent: for any reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the object is modified. For any non-null reference value x, x.equals(null) should return false. The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).

hashCode

public int hashCode()
Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable. The general contract of hashCode is: Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables. As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

isMaintainAspectRatio

public boolean isMaintainAspectRatio()

Returns

the maintainAspectRatio

setMaintainAspectRatio

public void setMaintainAspectRatio(boolean maintainAspectRatio)

Parameters

maintainAspectRatio boolean
the maintainAspectRatio to set