ImageDecoder | API reference | Android Developers
A class for converting encoded images (like PNG, JPEG,
WEBP, GIF, or HEIF) into Drawable or
Bitmap objects.
The ImageInfo contains information about the encoded image, like
its width and height, and the Source can be used to match to a particular
Source if a single OnHeaderDecodedListener
is used with multiple Source objects.
Nested classes
class
ImageDecoder.DecodeException
Information about an interrupted decode.
class
ImageDecoder.ImageInfo
Information about an encoded image.
interface
ImageDecoder.OnHeaderDecodedListener
Interface for changing the default settings of a decode.
interface
ImageDecoder.OnPartialImageListener
Interface for inspecting a DecodeException
and potentially preventing it from being thrown.
class
ImageDecoder.Source
Source of encoded image data.
Constants | |
|---|---|
int |
ALLOCATOR_DEFAULT
Use the default allocation for the pixel memory. |
int |
ALLOCATOR_HARDWARE
Require a |
int |
ALLOCATOR_SHARED_MEMORY
Use shared memory for the pixel memory. |
int |
ALLOCATOR_SOFTWARE
Use a software allocation for the pixel memory. |
int |
MEMORY_POLICY_DEFAULT
Use the most natural |
int |
MEMORY_POLICY_LOW_RAM
Save memory if possible by using a denser |
Public methods | |
|---|---|
void
|
close()
Closes this resource, relinquishing any underlying resources. |
static
ImageDecoder.Source
|
createSource(byte[] data)
Create a new |
static
ImageDecoder.Source
|
createSource(AssetManager assets, String fileName)
Create a new |
static
ImageDecoder.Source
|
createSource(Callable<AssetFileDescriptor> callable)
Create a new |
static
ImageDecoder.Source
|
createSource(ByteBuffer buffer)
Create a new |
static
ImageDecoder.Source
|
createSource(File file)
|
static
ImageDecoder.Source
|
createSource(Resources res, int resId)
Create a new |
static
ImageDecoder.Source
|
createSource(ContentResolver cr, Uri uri)
|
static
ImageDecoder.Source
|
createSource(byte[] data, int offset, int length)
Create a new |
static
Bitmap
|
decodeBitmap(ImageDecoder.Source src, ImageDecoder.OnHeaderDecodedListener listener)
Create a |
static
Bitmap
|
decodeBitmap(ImageDecoder.Source src)
Create a |
static
Drawable
|
decodeDrawable(ImageDecoder.Source src)
Create a |
static
Drawable
|
decodeDrawable(ImageDecoder.Source src, ImageDecoder.OnHeaderDecodedListener listener)
Create a |
int
|
getAllocator()
Return the allocator for the pixel memory. |
Rect
|
getCrop()
Return the cropping rectangle, if set. |
static
ImageDecoder.OnHeaderDecodedListener
|
getDefaultProcessListener()
Return the process-level default |
static
ImageDecoder.OnHeaderDecodedListener
|
getDefaultThreadListener()
Return the thread-level default |
int
|
getMemorySizePolicy()
Retrieve the memory policy for the decoded |
ImageDecoder.OnPartialImageListener
|
getOnPartialImageListener()
Return the |
PostProcessor
|
getPostProcessor()
Return the |
boolean
|
isDecodeAsAlphaMaskEnabled()
Return whether to treat single channel input as alpha. |
static
boolean
|
isMimeTypeSupported(String mimeType)
Return if the given MIME type is a supported file format that can be decoded by this class. |
boolean
|
isMutableRequired()
Return whether the decoded |
boolean
|
isUnpremultipliedRequired()
Return whether the |
void
|
setAllocator(int allocator)
Choose the backing for the pixel memory. |
void
|
setCrop(Rect subset)
Crop the output to |
void
|
setDecodeAsAlphaMaskEnabled(boolean enabled)
Specify whether to potentially treat the output as an alpha mask. |
static
void
|
setDefaultProcessListener(ImageDecoder.OnHeaderDecodedListener listener)
Set the process-level default |
static
void
|
setDefaultThreadListener(ImageDecoder.OnHeaderDecodedListener listener)
Set the thread-level default |
void
|
setMemorySizePolicy(int policy)
Specify the memory policy for the decoded |
void
|
setMutableRequired(boolean mutable)
Specify whether the |
void
|
setOnPartialImageListener(ImageDecoder.OnPartialImageListener listener)
Set (replace) the |
void
|
setPostProcessor(PostProcessor postProcessor)
Modify the image after decoding and scaling. |
void
|
setTargetColorSpace(ColorSpace colorSpace)
Specify the desired |
void
|
setTargetSampleSize(int sampleSize)
Set the target size with a sampleSize. |
void
|
setTargetSize(int width, int height)
|
void
|
setUnpremultipliedRequired(boolean unpremultipliedRequired)
Specify whether the |
Protected methods | |
|---|---|
void
|
finalize()
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. |
Inherited methods | |||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
From class
| |||||||||||||||||||||||
|
From interface
| |||||||||||||||||||||||
Constants
ALLOCATOR_SHARED_MEMORY
public static final int ALLOCATOR_SHARED_MEMORY
Use shared memory for the pixel memory.
Useful for sharing across processes.
Constant Value: 2 (0x00000002)
ALLOCATOR_SOFTWARE
public static final int ALLOCATOR_SOFTWARE
Use a software allocation for the pixel memory.
Useful for drawing to a software Canvas or for
accessing the pixels on the final output.
Constant Value: 1 (0x00000001)
MEMORY_POLICY_DEFAULT
public static final int MEMORY_POLICY_DEFAULT
Use the most natural Bitmap.Config for the internal Bitmap.
This is the recommended default for most applications and usages. This
will use the closest Bitmap.Config for the encoded source. If the
encoded source does not exactly match any Bitmap.Config, the next
highest quality Bitmap.Config will be used avoiding any loss in
image quality.
Constant Value: 1 (0x00000001)
MEMORY_POLICY_LOW_RAM
public static final int MEMORY_POLICY_LOW_RAM
Save memory if possible by using a denser Bitmap.Config at the
cost of some image quality.
For example an opaque 8-bit image may be compressed into an
Bitmap.Config.RGB_565 configuration, sacrificing image
quality to save memory.
Constant Value: 0 (0x00000000)
Public methods
close
public void close ()
Closes this resource, relinquishing any underlying resources. This method is invoked automatically on objects managed by the try-with-resources statement.
This is an implementation detail of ImageDecoder, and should
never be called manually.
createSource
public static ImageDecoder.Source createSource (byte[] data)
Create a new Source from a byte array.
Note: If this Source is passed to decodeDrawable,
and the encoded image is animated, the returned AnimatedImageDrawable
will continue reading from data, so its contents must not
be modified, even after the AnimatedImageDrawable is returned.
data's contents should never be modified during decode.
.
This method is safe to call from any thread.
| Parameters | |
|---|---|
data |
byte: byte array of compressed image data.
This value cannot be null. |
| Returns | |
|---|---|
ImageDecoder.Source |
a new Source object, which can be passed to
decodeDrawable or
decodeBitmap.
This value cannot be null. |
| Throws | |
|---|---|
NullPointerException |
if data is null. |
createSource
public static ImageDecoder.Source createSource (AssetManager assets, String fileName)
Create a new Source from a file in the "assets" directory.
This method is safe to call from any thread.
| Parameters | |
|---|---|
assets |
AssetManager: This value cannot be null. |
fileName |
String: This value cannot be null. |
| Returns | |
|---|---|
ImageDecoder.Source |
This value cannot be null. |
createSource
public static ImageDecoder.Source createSource (ByteBuffer buffer)
Create a new Source from a ByteBuffer.
Decoding will start from buffer.position().
The position of buffer will not be affected.
Note: If this Source is passed to decodeDrawable,
and the encoded image is animated, the returned AnimatedImageDrawable
will continue reading from the buffer, so its contents must not
be modified, even after the AnimatedImageDrawable is returned.
buffer's contents should never be modified during decode.
.
This method is safe to call from any thread.
| Parameters | |
|---|---|
buffer |
ByteBuffer: This value cannot be null. |
| Returns | |
|---|---|
ImageDecoder.Source |
a new Source object, which can be passed to
decodeDrawable or
decodeBitmap.
This value cannot be null. |
createSource
public static ImageDecoder.Source createSource (Resources res, int resId)
Create a new Source from a resource.
This method is safe to call from any thread.
| Parameters | |
|---|---|
res |
Resources: the Resources object containing the image data.
This value cannot be null. |
resId |
int: resource ID of the image data. |
| Returns | |
|---|---|
ImageDecoder.Source |
a new Source object, which can be passed to
decodeDrawable or
decodeBitmap.
This value cannot be null. |
createSource
public static ImageDecoder.Source createSource (byte[] data, int offset, int length)
Create a new Source from a byte array.
Note: If this Source is passed to decodeDrawable,
and the encoded image is animated, the returned AnimatedImageDrawable
will continue reading from data, so its contents must not
be modified, even after the AnimatedImageDrawable is returned.
data's contents should never be modified during decode.
.
This method is safe to call from any thread.
| Parameters | |
|---|---|
data |
byte: byte array of compressed image data.
This value cannot be null. |
offset |
int: offset into data for where the decoder should begin
parsing. |
length |
int: number of bytes, beginning at offset, to parse. |
| Returns | |
|---|---|
ImageDecoder.Source |
a new Source object, which can be passed to
decodeDrawable or
decodeBitmap.
This value cannot be null. |
| Throws | |
|---|---|
ArrayIndexOutOfBoundsException |
if offset and length are not within data. |
NullPointerException |
if data is null. |
decodeBitmap
public static Bitmap decodeBitmap (ImageDecoder.Source src, ImageDecoder.OnHeaderDecodedListener listener)
Create a Bitmap from a Source.
This method may take several seconds to complete, so it should
only be called from a worker thread.
| Parameters | |
|---|---|
src |
ImageDecoder.Source: representing the encoded image.
This value cannot be null. |
listener |
ImageDecoder.OnHeaderDecodedListener: for learning the ImageInfo and changing any
default settings on the ImageDecoder. This will be called on
the same thread as decodeBitmap before that method returns.
This is required in order to change any of the default settings.
This value cannot be null. |
| Returns | |
|---|---|
Bitmap |
Bitmap containing the image.
This value cannot be null. |
| Throws | |
|---|---|
IOException |
if src is not found, is an unsupported
format, or cannot be decoded for any reason. |
decodeBitmap
public static Bitmap decodeBitmap (ImageDecoder.Source src)
Create a Bitmap from a Source.
Since there is no OnHeaderDecodedListener,
the default settings will be used. In order to change any settings, call
decodeBitmap(Source,OnHeaderDecodedListener) instead.
.
This method may take several seconds to complete, so it should
only be called from a worker thread.
| Parameters | |
|---|---|
src |
ImageDecoder.Source: representing the encoded image.
This value cannot be null. |
| Returns | |
|---|---|
Bitmap |
Bitmap containing the image.
This value cannot be null. |
| Throws | |
|---|---|
IOException |
if src is not found, is an unsupported
format, or cannot be decoded for any reason. |
decodeDrawable
public static Drawable decodeDrawable (ImageDecoder.Source src)
Create a Drawable from a Source.
Since there is no OnHeaderDecodedListener,
the default settings will be used. In order to change any settings, call
decodeDrawable(Source,OnHeaderDecodedListener) instead.
.
This method may take several seconds to complete, so it should
only be called from a worker thread.
| Parameters | |
|---|---|
src |
ImageDecoder.Source: representing the encoded image.
This value cannot be null. |
| Returns | |
|---|---|
Drawable |
Drawable for displaying the image.
This value cannot be null. |
| Throws | |
|---|---|
IOException |
if src is not found, is an unsupported
format, or cannot be decoded for any reason. |
decodeDrawable
public static Drawable decodeDrawable (ImageDecoder.Source src, ImageDecoder.OnHeaderDecodedListener listener)
Create a Drawable from a Source.
This method may take several seconds to complete, so it should
only be called from a worker thread.
| Parameters | |
|---|---|
src |
ImageDecoder.Source: representing the encoded image.
This value cannot be null. |
listener |
ImageDecoder.OnHeaderDecodedListener: for learning the ImageInfo and changing any
default settings on the ImageDecoder. This will be called on
the same thread as decodeDrawable before that method returns.
This is required in order to change any of the default settings.
This value cannot be null. |
| Returns | |
|---|---|
Drawable |
Drawable for displaying the image.
This value cannot be null. |
| Throws | |
|---|---|
IOException |
if src is not found, is an unsupported
format, or cannot be decoded for any reason. |
getCrop
public Rect getCrop ()
Return the cropping rectangle, if set.
| Returns | |
|---|---|
Rect |
This value may be null. |
isDecodeAsAlphaMaskEnabled
public boolean isDecodeAsAlphaMaskEnabled ()
Return whether to treat single channel input as alpha.
This returns whether setDecodeAsAlphaMaskEnabled(boolean) was set to
true. It may still return true even if the image has
more than one channel and therefore will not be treated as an alpha
mask.
| Returns | |
|---|---|
boolean |
|
isMimeTypeSupported
public static boolean isMimeTypeSupported (String mimeType)
Return if the given MIME type is a supported file format that can be
decoded by this class. This can be useful to determine if a file can be
decoded directly, or if it needs to be converted into a more general
format using an API like ContentResolver.openTypedAssetFile.
| Parameters | |
|---|---|
mimeType |
String: This value cannot be null. |
| Returns | |
|---|---|
boolean |
|
isMutableRequired
public boolean isMutableRequired ()
Return whether the decoded Bitmap will be mutable.
| Returns | |
|---|---|
boolean |
|
isUnpremultipliedRequired
public boolean isUnpremultipliedRequired ()
Return whether the Bitmap will have unpremultiplied pixels.
| Returns | |
|---|---|
boolean |
|
setDecodeAsAlphaMaskEnabled
public void setDecodeAsAlphaMaskEnabled (boolean enabled)
Specify whether to potentially treat the output as an alpha mask.
If this is set to true and the image is encoded in a format
with only one channel, treat that channel as alpha. Otherwise this call has
no effect.
This is incompatible with ALLOCATOR_HARDWARE. Trying to
combine them will result in decodeDrawable/
decodeBitmap throwing an
IllegalStateException.
Like all setters on ImageDecoder, this must be called inside
onHeaderDecoded.
| Parameters | |
|---|---|
enabled |
boolean |
setOnPartialImageListener
public void setOnPartialImageListener (ImageDecoder.OnPartialImageListener listener)
Set (replace) the OnPartialImageListener on this object.
Will be called if there is an error in the input. Without one, an
error will result in an Exception being thrown.
Like all setters on ImageDecoder, this must be called inside
onHeaderDecoded.
| Parameters | |
|---|---|
listener |
ImageDecoder.OnPartialImageListener: This value may be null. |
setPostProcessor
public void setPostProcessor (PostProcessor postProcessor)
Modify the image after decoding and scaling.
This allows adding effects prior to returning a Drawable or
Bitmap. For a Drawable or an immutable Bitmap,
this is the only way to process the image after decoding.
If combined with setTargetSize(int, int) and/or setCrop(Rect),
PostProcessor.onPostProcess occurs last.
If set on a nine-patch image, the nine-patch data is ignored.
For an animated image, the drawing commands drawn on the
Canvas will be recorded immediately and then applied to each
frame.
Like all setters on ImageDecoder, this must be called inside
onHeaderDecoded.
| Parameters | |
|---|---|
postProcessor |
PostProcessor: This value may be null. |
setTargetSampleSize
public void setTargetSampleSize (int sampleSize)
Set the target size with a sampleSize.
By default, the output size will match the size of the encoded
image, which can be retrieved from the ImageInfo in
onHeaderDecoded.
Requests the decoder to subsample the original image, returning a
smaller image to save memory. The sampleSize is the number of pixels
in either dimension that correspond to a single pixel in the output.
For example, sampleSize == 4 returns an image that is 1/4 the
width/height of the original, and 1/16 the number of pixels.
Must be greater than or equal to 1.
This has the same effect as calling setTargetSize(int, int) with
dimensions based on the sampleSize. Unlike dividing the original
width and height by the sampleSize manually, calling this method
allows ImageDecoder to round in the direction that it can do most
efficiently.
Only the last call to this or setTargetSize(int, int) is respected.
Like all setters on ImageDecoder, this must be called inside
onHeaderDecoded.
| Parameters | |
|---|---|
sampleSize |
int: sampling rate of the encoded image.
Value is 1 or greater |
setTargetSize
public void setTargetSize (int width,
int height)
Specify the size of the output Drawable or Bitmap.
By default, the output size will match the size of the encoded
image, which can be retrieved from the ImageInfo in
onHeaderDecoded.
This will sample or scale the output to an arbitrary size that may be smaller or larger than the encoded size.
Only the last call to this or setTargetSampleSize(int) is
respected.
Like all setters on ImageDecoder, this must be called inside
onHeaderDecoded.
| Parameters | |
|---|---|
width |
int: width in pixels of the output, must be greater than 0.
The units of this value are pixels. Value is 1 or greater |
height |
int: height in pixels of the output, must be greater than 0.
The units of this value are pixels. Value is 1 or greater |
setUnpremultipliedRequired
public void setUnpremultipliedRequired (boolean unpremultipliedRequired)
Specify whether the Bitmap should have unpremultiplied pixels.
By default, ImageDecoder will create a Bitmap with
premultiplied pixels, which is required for drawing with the
View system (i.e. to a Canvas). Calling
this method with a value of true will result in
decodeBitmap(Source) returning a Bitmap with unpremultiplied
pixels. See Bitmap.isPremultiplied().
This is incompatible with decodeDrawable;
attempting to decode an unpremultiplied Drawable will throw an
IllegalStateException.
Like all setters on ImageDecoder, this must be called inside
onHeaderDecoded.
| Parameters | |
|---|---|
unpremultipliedRequired |
boolean |
Protected methods
finalize
protected void finalize ()
Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.
A subclass overrides the finalize method to dispose of
system resources or to perform other cleanup.
The general contract of finalize is that it is invoked
if and when the Java virtual
machine has determined that there is no longer any
means by which this object can be accessed by any thread that has
not yet died, except as a result of an action taken by the
finalization of some other object or class which is ready to be
finalized. The finalize method may take any action, including
making this object available again to other threads; the usual purpose
of finalize, however, is to perform cleanup actions before
the object is irrevocably discarded. For example, the finalize method
for an object that represents an input/output connection might perform
explicit I/O transactions to break the connection before the object is
permanently discarded.
The finalize method of class Object performs no
special action; it simply returns normally. Subclasses of
Object may override this definition.
The Java programming language does not guarantee which thread will
invoke the finalize method for any given object. It is
guaranteed, however, that the thread that invokes finalize will not
be holding any user-visible synchronization locks when finalize is
invoked. If an uncaught exception is thrown by the finalize method,
the exception is ignored and finalization of that object terminates.
After the finalize method has been invoked for an object, no
further action is taken until the Java virtual machine has again
determined that there is no longer any means by which this object can
be accessed by any thread that has not yet died, including possible
actions by other objects or classes which are ready to be finalized,
at which point the object may be discarded.
The finalize method is never invoked more than once by a Java
virtual machine for any given object.
Any exception thrown by the finalize method causes
the finalization of this object to be halted, but is otherwise
ignored.
| Throws | |
|---|---|
Throwable |
|