Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)
Android APIs
public class

BitmapFactory

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

Class Overview

Creates Bitmap objects from various sources, including files, streams, and byte-arrays.

Summary

Nested Classes
class BitmapFactory.Options  
Public Constructors
BitmapFactory ()
Public Methods
static Bitmap decodeByteArray (byte[] data, int offset, int length, BitmapFactory.Options opts)
Decode an immutable bitmap from the specified byte array.
static Bitmap decodeByteArray (byte[] data, int offset, int length)
Decode an immutable bitmap from the specified byte array.
static Bitmap decodeFile ( String pathName)
Decode a file path into a bitmap.
static Bitmap decodeFile ( String pathName, BitmapFactory.Options opts)
Decode a file path into a bitmap.
static Bitmap decodeFileDescriptor ( FileDescriptor fd)
Decode a bitmap from the file descriptor.
static Bitmap decodeFileDescriptor ( FileDescriptor fd, Rect outPadding, BitmapFactory.Options opts)
Decode a bitmap from the file descriptor.
static Bitmap decodeResource ( Resources res, int id, BitmapFactory.Options opts)
static Bitmap decodeResource ( Resources res, int id)
static Bitmap decodeResourceStream ( Resources res, TypedValue value, InputStream is, Rect pad, BitmapFactory.Options opts)
Decode a new Bitmap from an InputStream.
static Bitmap decodeStream ( InputStream is)
Decode an input stream into a bitmap.
static Bitmap decodeStream ( InputStream is, Rect outPadding, BitmapFactory.Options opts)
Decode an input stream into a bitmap.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public BitmapFactory ()

Added in API level 1

Public Methods

public static Bitmap decodeByteArray (byte[] data, int offset, int length, BitmapFactory.Options opts)

Added in API level 1

Decode an immutable bitmap from the specified byte array.

Parameters
data byte array of compressed image data
offset offset into imageData for where the decoder should begin parsing.
length the number of bytes, beginning at offset, to parse
opts null-ok; Options that control downsampling and whether the image should be completely decoded, or just is size returned.
Returns
  • The decoded bitmap, or null if the image data could not be decoded, or, if opts is non-null, if opts requested only the size be returned (in opts.outWidth and opts.outHeight)

public static Bitmap decodeByteArray (byte[] data, int offset, int length)

Added in API level 1

Decode an immutable bitmap from the specified byte array.

Parameters
data byte array of compressed image data
offset offset into imageData for where the decoder should begin parsing.
length the number of bytes, beginning at offset, to parse
Returns
  • The decoded bitmap, or null if the image could not be decoded.

public static Bitmap decodeFile ( String pathName)

Added in API level 1

Decode a file path into a bitmap. If the specified file name is null, or cannot be decoded into a bitmap, the function returns null.

Parameters
pathName complete path name for the file to be decoded.
Returns
  • the resulting decoded bitmap, or null if it could not be decoded.

public static Bitmap decodeFile ( String pathName, BitmapFactory.Options opts)

Added in API level 1

Decode a file path into a bitmap. If the specified file name is null, or cannot be decoded into a bitmap, the function returns null.

Parameters
pathName complete path name for the file to be decoded.
opts null-ok; Options that control downsampling and whether the image should be completely decoded, or just is size returned.
Returns
  • The decoded bitmap, or null if the image data could not be decoded, or, if opts is non-null, if opts requested only the size be returned (in opts.outWidth and opts.outHeight)

public static Bitmap decodeFileDescriptor ( FileDescriptor fd)

Added in API level 1

Decode a bitmap from the file descriptor. If the bitmap cannot be decoded return null. The position within the descriptor will not be changed when this returns, so the descriptor can be used again as is.

Parameters
fd The file descriptor containing the bitmap data to decode
Returns
  • the decoded bitmap, or null

public static Bitmap decodeFileDescriptor ( FileDescriptor fd, Rect outPadding, BitmapFactory.Options opts)

Added in API level 1

Decode a bitmap from the file descriptor. If the bitmap cannot be decoded return null. The position within the descriptor will not be changed when this returns, so the descriptor can be used again as-is.

Parameters
fd The file descriptor containing the bitmap data to decode
outPadding If not null, return the padding rect for the bitmap if it exists, otherwise set padding to [-1,-1,-1,-1]. If no bitmap is returned (null) then padding is unchanged.
opts null-ok; Options that control downsampling and whether the image should be completely decoded, or just its size returned.
Returns
  • the decoded bitmap, or null

public static Bitmap decodeResource ( Resources res, int id, BitmapFactory.Options opts)

Added in API level 1
Parameters
res The resources object containing the image data
id The resource id of the image data
opts null-ok; Options that control downsampling and whether the image should be completely decoded, or just is size returned.
Returns
  • The decoded bitmap, or null if the image data could not be decoded, or, if opts is non-null, if opts requested only the size be returned (in opts.outWidth and opts.outHeight)

public static Bitmap decodeResource ( Resources res, int id)

Added in API level 1
Parameters
res The resources object containing the image data
id The resource id of the image data
Returns
  • The decoded bitmap, or null if the image could not be decoded.

public static Bitmap decodeResourceStream ( Resources res, TypedValue value, InputStream is, Rect pad, BitmapFactory.Options opts)

Added in API level 4

Decode a new Bitmap from an InputStream. This InputStream was obtained from resources, which we pass to be able to scale the bitmap accordingly.

public static Bitmap decodeStream ( InputStream is)

Added in API level 1

Decode an input stream into a bitmap. If the input stream is null, or cannot be used to decode a bitmap, the function returns null. The stream's position will be where ever it was after the encoded data was read.

Parameters
is The input stream that holds the raw data to be decoded into a bitmap.
Returns
  • The decoded bitmap, or null if the image data could not be decoded.

public static Bitmap decodeStream ( InputStream is, Rect outPadding, BitmapFactory.Options opts)

Added in API level 1

Decode an input stream into a bitmap. If the input stream is null, or cannot be used to decode a bitmap, the function returns null. The stream's position will be where ever it was after the encoded data was read.

Parameters
is The input stream that holds the raw data to be decoded into a bitmap.
outPadding If not null, return the padding rect for the bitmap if it exists, otherwise set padding to [-1,-1,-1,-1]. If no bitmap is returned (null) then padding is unchanged.
opts null-ok; Options that control downsampling and whether the image should be completely decoded, or just is size returned.
Returns
  • The decoded bitmap, or null if the image data could not be decoded, or, if opts is non-null, if opts requested only the size be returned (in opts.outWidth and opts.outHeight)

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