Android APIs
public final class

BitmapRegionDecoder

extends Object
java.lang.Object
   ↳ android.graphics.BitmapRegionDecoder

Class Overview

BitmapRegionDecoder can be used to decode a rectangle region from an image. BitmapRegionDecoder is particularly useful when an original image is large and you only need parts of the image.

To create a BitmapRegionDecoder, call newInstance(...). Given a BitmapRegionDecoder, users can call decodeRegion() repeatedly to get a decoded Bitmap of the specified region.

Summary

Public Methods
Bitmap decodeRegion(Rect rect, BitmapFactory.Options options)
Decodes a rectangle region in the image specified by rect.
int getHeight()
Returns the original image's height
int getWidth()
Returns the original image's width
final boolean isRecycled()
Returns true if this region decoder has been recycled.
static BitmapRegionDecoder newInstance(String pathName, boolean isShareable)
Create a BitmapRegionDecoder from a file path.
static BitmapRegionDecoder newInstance(InputStream is, boolean isShareable)
Create a BitmapRegionDecoder from an input stream.
static BitmapRegionDecoder newInstance(FileDescriptor fd, boolean isShareable)
Create a BitmapRegionDecoder from the file descriptor.
static BitmapRegionDecoder newInstance(byte[] data, int offset, int length, boolean isShareable)
Create a BitmapRegionDecoder from the specified byte array.
void recycle()
Frees up the memory associated with this region decoder, and mark the region decoder as "dead", meaning it will throw an exception if decodeRegion(), getWidth() or getHeight() is called.
Protected Methods
void finalize()
Invoked when the garbage collector has detected that this instance is no longer reachable.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public Bitmap decodeRegion (Rect rect, BitmapFactory.Options options)

Added in API level 10

Decodes a rectangle region in the image specified by rect.

Parameters
rect The rectangle that specified the region to be decode.
options null-ok; Options that control downsampling. inPurgeable is not supported.
Returns
  • The decoded bitmap, or null if the image data could not be decoded.

public int getHeight ()

Added in API level 10

Returns the original image's height

public int getWidth ()

Added in API level 10

Returns the original image's width

public final boolean isRecycled ()

Added in API level 10

Returns true if this region decoder has been recycled. If so, then it is an error to try use its method.

Returns
  • true if the region decoder has been recycled

public static BitmapRegionDecoder newInstance (String pathName, boolean isShareable)

Added in API level 10

Create a BitmapRegionDecoder from a file path. Currently only the JPEG and PNG formats are supported.

Parameters
pathName complete path name for the file to be decoded.
isShareable If this is true, then the BitmapRegionDecoder may keep a shallow reference to the input. If this is false, then the BitmapRegionDecoder will explicitly make a copy of the input data, and keep that. Even if sharing is allowed, the implementation may still decide to make a deep copy of the input data. If an image is progressively encoded, allowing sharing may degrade the decoding speed.
Returns
  • BitmapRegionDecoder, or null if the image data could not be decoded.
Throws
IOException if the image format is not supported or can not be decoded.

public static BitmapRegionDecoder newInstance (InputStream is, boolean isShareable)

Added in API level 10

Create a BitmapRegionDecoder from an input stream. The stream's position will be where ever it was after the encoded data was read. Currently only the JPEG and PNG formats are supported.

Parameters
is The input stream that holds the raw data to be decoded into a BitmapRegionDecoder.
isShareable If this is true, then the BitmapRegionDecoder may keep a shallow reference to the input. If this is false, then the BitmapRegionDecoder will explicitly make a copy of the input data, and keep that. Even if sharing is allowed, the implementation may still decide to make a deep copy of the input data. If an image is progressively encoded, allowing sharing may degrade the decoding speed.
Returns
  • BitmapRegionDecoder, or null if the image data could not be decoded.
Throws
IOException if the image format is not supported or can not be decoded.

Prior to KITKAT, if is.markSupported() returns true, is.mark(1024) would be called. As of KITKAT, this is no longer the case.

public static BitmapRegionDecoder newInstance (FileDescriptor fd, boolean isShareable)

Added in API level 10

Create a BitmapRegionDecoder from the file descriptor. The position within the descriptor will not be changed when this returns, so the descriptor can be used again as is. Currently only the JPEG and PNG formats are supported.

Parameters
fd The file descriptor containing the data to decode
isShareable If this is true, then the BitmapRegionDecoder may keep a shallow reference to the input. If this is false, then the BitmapRegionDecoder will explicitly make a copy of the input data, and keep that. Even if sharing is allowed, the implementation may still decide to make a deep copy of the input data. If an image is progressively encoded, allowing sharing may degrade the decoding speed.
Returns
  • BitmapRegionDecoder, or null if the image data could not be decoded.
Throws
IOException if the image format is not supported or can not be decoded.

public static BitmapRegionDecoder newInstance (byte[] data, int offset, int length, boolean isShareable)

Added in API level 10

Create a BitmapRegionDecoder from the specified byte array. Currently only the JPEG and PNG formats are supported.

Parameters
data byte array of compressed image data.
offset offset into data for where the decoder should begin parsing.
length the number of bytes, beginning at offset, to parse
isShareable If this is true, then the BitmapRegionDecoder may keep a shallow reference to the input. If this is false, then the BitmapRegionDecoder will explicitly make a copy of the input data, and keep that. Even if sharing is allowed, the implementation may still decide to make a deep copy of the input data. If an image is progressively encoded, allowing sharing may degrade the decoding speed.
Returns
  • BitmapRegionDecoder, or null if the image data could not be decoded.
Throws
IOException if the image format is not supported or can not be decoded.

public void recycle ()

Added in API level 10

Frees up the memory associated with this region decoder, and mark the region decoder as "dead", meaning it will throw an exception if decodeRegion(), getWidth() or getHeight() is called.

This operation cannot be reversed, so it should only be called if you are sure there are no further uses for the region decoder. This is an advanced call, and normally need not be called, since the normal GC process will free up this memory when there are no more references to this region decoder.

Protected Methods

protected void finalize ()

Added in API level 10

Invoked when the garbage collector has detected that this instance is no longer reachable. The default implementation does nothing, but this method can be overridden to free resources.

Note that objects that override finalize are significantly more expensive than objects that don't. Finalizers may be run a long time after the object is no longer reachable, depending on memory pressure, so it's a bad idea to rely on them for cleanup. Note also that finalizers are run on a single VM-wide finalizer thread, so doing blocking work in a finalizer is a bad idea. A finalizer is usually only necessary for a class that has a native peer and needs to call a native method to destroy that peer. Even then, it's better to provide an explicit close method (and implement Closeable), and insist that callers manually dispose of instances. This works well for something like files, but less well for something like a BigInteger where typical calling code would have to deal with lots of temporaries. Unfortunately, code that creates lots of temporaries is the worst kind of code from the point of view of the single finalizer thread.

If you must use finalizers, consider at least providing your own ReferenceQueue and having your own thread process that queue.

Unlike constructors, finalizers are not automatically chained. You are responsible for calling super.finalize() yourself.

Uncaught exceptions thrown by finalizers are ignored and do not terminate the finalizer thread. See Effective Java Item 7, "Avoid finalizers" for more.

Throws
Throwable