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()
Called before the object's memory is reclaimed by the VM.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

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

Since: 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 ()

Since: API Level 10

Returns the original image's height

public int getWidth ()

Since: API Level 10

Returns the original image's width

public final boolean isRecycled ()

Since: 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)

Since: 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)

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

public static BitmapRegionDecoder newInstance (FileDescriptor fd, boolean isShareable)

Since: 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)

Since: 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 ()

Since: 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 ()

Since: API Level 10

Called before the object's memory is reclaimed by the VM. This can only happen once the garbage collector has detected that the object is no longer reachable by any thread of the running application.

The method can be used to free system resources or perform other cleanup before the object is garbage collected. The default implementation of the method is empty, which is also expected by the VM, but subclasses can override finalize() as required. Uncaught exceptions which are thrown during the execution of this method cause it to terminate immediately but are otherwise ignored.

Note that the VM does guarantee that finalize() is called at most once for any object, but it doesn't guarantee when (if at all) finalize() will be called. For example, object B's finalize() can delay the execution of object A's finalize() method and therefore it can delay the reclamation of A's memory. To be safe, use a ReferenceQueue, because it provides more control over the way the VM deals with references during garbage collection.

Throws
Throwable