FileDocCategorySizeDatePackage
BitmapFactory.javaAPI DocAndroid 1.5 API17883Wed May 06 22:42:00 BST 2009android.graphics

BitmapFactory

public class BitmapFactory extends Object
Creates Bitmap objects from various sources, including files, streams, and byte-arrays.

Fields Summary
Constructors Summary
Methods Summary
public static BitmapdecodeByteArray(byte[] data, int offset, int length, android.graphics.BitmapFactory$Options opts)
Decode an immutable bitmap from the specified byte array.

param
data byte array of compressed image data
param
offset offset into imageData for where the decoder should begin parsing.
param
length the number of bytes, beginning at offset, to parse
param
opts null-ok; Options that control downsampling and whether the image should be completely decoded, or just is size returned.
return
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)

        if ((offset | length) < 0 || data.length < offset + length) {
            throw new ArrayIndexOutOfBoundsException();
        }
        return nativeDecodeByteArray(data, offset, length, opts);
    
public static BitmapdecodeByteArray(byte[] data, int offset, int length)
Decode an immutable bitmap from the specified byte array.

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

        return decodeByteArray(data, offset, length, null);
    
public static BitmapdecodeFile(java.lang.String pathName, android.graphics.BitmapFactory$Options opts)
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.

param
pathName complete path name for the file to be decoded.
param
opts null-ok; Options that control downsampling and whether the image should be completely decoded, or just is size returned.
return
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)

        Bitmap bm = null;
        InputStream stream = null;
        try {
            stream = new FileInputStream(pathName);
            bm = decodeStream(stream, null, opts);
        } catch (Exception e) {
            /*  do nothing.
                If the exception happened on open, bm will be null.
            */
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                    // do nothing here
                }
            }
        }
        return bm;
    
public static BitmapdecodeFile(java.lang.String pathName)
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.

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

        return decodeFile(pathName, null);
    
public static BitmapdecodeFileDescriptor(java.io.FileDescriptor fd, Rect outPadding, android.graphics.BitmapFactory$Options opts)
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.

param
fd The file descriptor containing the bitmap data to decode
param
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.
param
opts null-ok; Options that control downsampling and whether the image should be completely decoded, or just is size returned.
return
the decoded bitmap, or null

        return nativeDecodeFileDescriptor(fd, outPadding, opts);
    
public static BitmapdecodeFileDescriptor(java.io.FileDescriptor fd)
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.

param
fd The file descriptor containing the bitmap data to decode
return
the decoded bitmap, or null

        return nativeDecodeFileDescriptor(fd, null, null);
    
public static BitmapdecodeResource(android.content.res.Resources res, int id, android.graphics.BitmapFactory$Options opts)
Decode an image referenced by a resource ID.

param
res The resources object containing the image data
param
id The resource id of the image data
param
opts null-ok; Options that control downsampling and whether the image should be completely decoded, or just is size returned.
return
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)

        Bitmap bm = null;
        
        try {
            final TypedValue value = new TypedValue();
            final InputStream is = res.openRawResource(id, value);

            bm = decodeStream(res, value, is, null, opts);
            is.close();
        } catch (java.io.IOException e) {
            /*  do nothing.
                If the exception happened on open, bm will be null.
                If it happened on close, bm is still valid.
            */
        }
        return bm;
    
public static BitmapdecodeResource(android.content.res.Resources res, int id)
Decode an image referenced by a resource ID.

param
res The resources object containing the image data
param
id The resource id of the image data
return
The decoded bitmap, or null if the image could not be decode.

        return decodeResource(res, id, null);
    
public static BitmapdecodeStream(android.content.res.Resources res, android.util.TypedValue value, java.io.InputStream is, Rect pad, android.graphics.BitmapFactory$Options opts)
Decode a new Bitmap from an InputStream. This InputStream was obtained from resources, which we pass to be able to scale the bitmap accordingly.

hide


        if (opts == null) {
            opts = new Options();
        }

        Bitmap bm = decodeStream(is, pad, opts);

        if (bm != null && res != null && value != null) {
            byte[] np = bm.getNinePatchChunk();
            final boolean isNinePatch = np != null && NinePatch.isNinePatchChunk(np);

            final int density = value.density;
            if (opts.inDensity == 0) {
                opts.inDensity = density == TypedValue.DENSITY_DEFAULT ?
                        DisplayMetrics.DEFAULT_DENSITY : density;
            }            
            float scale = opts.inDensity / (float) DisplayMetrics.DEFAULT_DENSITY;

            if (opts.inScaled || isNinePatch) {
                bm.setDensityScale(1.0f);
                bm.setAutoScalingEnabled(false);
                // Assume we are going to prescale for the screen
                scale = res.getDisplayMetrics().density / scale;
                if (scale != 1.0f) {
                    // TODO: This is very inefficient and should be done in native by Skia
                    final Bitmap oldBitmap = bm;
                    bm = Bitmap.createScaledBitmap(oldBitmap, (int) (bm.getWidth() * scale + 0.5f),
                            (int) (bm.getHeight() * scale + 0.5f), true);
                    oldBitmap.recycle();

                    if (isNinePatch) {
                        np = nativeScaleNinePatch(np, scale, pad);
                        bm.setNinePatchChunk(np);
                    }
                }
            } else {
                bm.setDensityScale(scale);
                bm.setAutoScalingEnabled(true);
            }
        }

        return bm;
    
public static BitmapdecodeStream(java.io.InputStream is, Rect outPadding, android.graphics.BitmapFactory$Options opts)
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.

param
is The input stream that holds the raw data to be decoded into a bitmap.
param
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.
param
opts null-ok; Options that control downsampling and whether the image should be completely decoded, or just is size returned.
return
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)

        // we don't throw in this case, thus allowing the caller to only check
        // the cache, and not force the image to be decoded.
        if (is == null) {
            return null;
        }
        
        // we need mark/reset to work properly

        if (!is.markSupported()) {
            is = new BufferedInputStream(is, 16 * 1024);
        }

        // so we can call reset() if a given codec gives up after reading up to
        // this many bytes. FIXME: need to find out from the codecs what this
        // value should be.
        is.mark(1024);

        Bitmap  bm;

        if (is instanceof AssetManager.AssetInputStream) {
            bm = nativeDecodeAsset(((AssetManager.AssetInputStream) is).getAssetInt(),
                    outPadding, opts);
        } else {
            // pass some temp storage down to the native code. 1024 is made up,
            // but should be large enough to avoid too many small calls back
            // into is.read(...) This number is not related to the value passed
            // to mark(...) above.
            byte [] tempStorage = null;
            if (opts != null)
                tempStorage = opts.inTempStorage;
            if (tempStorage == null)
                tempStorage = new byte[16 * 1024];
            bm = nativeDecodeStream(is, tempStorage, outPadding, opts);
        }

        return bm;
    
public static BitmapdecodeStream(java.io.InputStream is)
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.

param
is The input stream that holds the raw data to be decoded into a bitmap.
return
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)

        return decodeStream(is, null, null);
    
private static native BitmapnativeDecodeAsset(int asset, Rect padding, android.graphics.BitmapFactory$Options opts)

private static native BitmapnativeDecodeByteArray(byte[] data, int offset, int length, android.graphics.BitmapFactory$Options opts)

private static native BitmapnativeDecodeFileDescriptor(java.io.FileDescriptor fd, Rect padding, android.graphics.BitmapFactory$Options opts)

private static native BitmapnativeDecodeStream(java.io.InputStream is, byte[] storage, Rect padding, android.graphics.BitmapFactory$Options opts)

private static native byte[]nativeScaleNinePatch(byte[] chunk, float scale, Rect pad)