FileDocCategorySizeDatePackage
BitmapFactory.javaAPI DocAndroid 5.1 API31226Thu Mar 12 22:22:30 GMT 2015android.graphics

BitmapFactory

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

Fields Summary
private static final int
DECODE_BUFFER_SIZE
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();
        }

        Bitmap bm;

        Trace.traceBegin(Trace.TRACE_TAG_GRAPHICS, "decodeBitmap");
        try {
            bm = nativeDecodeByteArray(data, offset, length, opts);

            if (bm == null && opts != null && opts.inBitmap != null) {
                throw new IllegalArgumentException("Problem decoding into existing bitmap");
            }
            setDensityFromOptions(bm, opts);
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_GRAPHICS);
        }

        return bm;
    
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 decoded.

        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.
            */
            Log.e("BitmapFactory", "Unable to decode stream: " + e);
        } 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 its size returned.
return
the decoded bitmap, or null

        Bitmap bm;

        Trace.traceBegin(Trace.TRACE_TAG_GRAPHICS, "decodeFileDescriptor");
        try {
            if (nativeIsSeekable(fd)) {
                bm = nativeDecodeFileDescriptor(fd, outPadding, opts);
            } else {
                FileInputStream fis = new FileInputStream(fd);
                try {
                    bm = decodeStreamInternal(fis, outPadding, opts);
                } finally {
                    try {
                        fis.close();
                    } catch (Throwable t) {/* ignore */}
                }
            }

            if (bm == null && opts != null && opts.inBitmap != null) {
                throw new IllegalArgumentException("Problem decoding into existing bitmap");
            }

            setDensityFromOptions(bm, opts);
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_GRAPHICS);
        }
        return bm;
    
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 decodeFileDescriptor(fd, null, null);
    
public static BitmapdecodeResource(android.content.res.Resources res, int id, android.graphics.BitmapFactory$Options opts)
Synonym for opening the given resource and calling {@link #decodeResourceStream}.

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;
        InputStream is = null; 
        
        try {
            final TypedValue value = new TypedValue();
            is = res.openRawResource(id, value);

            bm = decodeResourceStream(res, value, is, null, opts);
        } catch (Exception e) {
            /*  do nothing.
                If the exception happened on open, bm will be null.
                If it happened on close, bm is still valid.
            */
        } finally {
            try {
                if (is != null) is.close();
            } catch (IOException e) {
                // Ignore
            }
        }

        if (bm == null && opts != null && opts.inBitmap != null) {
            throw new IllegalArgumentException("Problem decoding into existing bitmap");
        }

        return bm;
    
public static BitmapdecodeResource(android.content.res.Resources res, int id)
Synonym for {@link #decodeResource(Resources, int, android.graphics.BitmapFactory.Options)} with null Options.

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 decoded.

        return decodeResource(res, id, null);
    
public static BitmapdecodeResourceStream(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.


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

        if (opts.inDensity == 0 && value != null) {
            final int density = value.density;
            if (density == TypedValue.DENSITY_DEFAULT) {
                opts.inDensity = DisplayMetrics.DENSITY_DEFAULT;
            } else if (density != TypedValue.DENSITY_NONE) {
                opts.inDensity = density;
            }
        }
        
        if (opts.inTargetDensity == 0 && res != null) {
            opts.inTargetDensity = res.getDisplayMetrics().densityDpi;
        }
        
        return decodeStream(is, pad, opts);
    
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.

        return decodeStream(is, null, null);
    
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)

Prior to {@link android.os.Build.VERSION_CODES#KITKAT}, if {@link InputStream#markSupported is.markSupported()} returns true, is.mark(1024) would be called. As of {@link android.os.Build.VERSION_CODES#KITKAT}, this is no longer the case.

        // 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;
        }

        Bitmap bm = null;

        Trace.traceBegin(Trace.TRACE_TAG_GRAPHICS, "decodeBitmap");
        try {
            if (is instanceof AssetManager.AssetInputStream) {
                final long asset = ((AssetManager.AssetInputStream) is).getNativeAsset();
                bm = nativeDecodeAsset(asset, outPadding, opts);
            } else {
                bm = decodeStreamInternal(is, outPadding, opts);
            }

            if (bm == null && opts != null && opts.inBitmap != null) {
                throw new IllegalArgumentException("Problem decoding into existing bitmap");
            }

            setDensityFromOptions(bm, opts);
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_GRAPHICS);
        }

        return bm;
    
private static BitmapdecodeStreamInternal(java.io.InputStream is, Rect outPadding, android.graphics.BitmapFactory$Options opts)
Private helper function for decoding an InputStream natively. Buffers the input enough to do a rewind as needed, and supplies temporary storage if necessary. is MUST NOT be null.

        // ASSERT(is != null);
        byte [] tempStorage = null;
        if (opts != null) tempStorage = opts.inTempStorage;
        if (tempStorage == null) tempStorage = new byte[DECODE_BUFFER_SIZE];
        return nativeDecodeStream(is, tempStorage, outPadding, opts);
    
private static native BitmapnativeDecodeAsset(long nativeAsset, 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 booleannativeIsSeekable(java.io.FileDescriptor fd)

private static voidsetDensityFromOptions(Bitmap outputBitmap, android.graphics.BitmapFactory$Options opts)
Set the newly decoded bitmap's density based on the Options.

        if (outputBitmap == null || opts == null) return;

        final int density = opts.inDensity;
        if (density != 0) {
            outputBitmap.setDensity(density);
            final int targetDensity = opts.inTargetDensity;
            if (targetDensity == 0 || density == targetDensity || density == opts.inScreenDensity) {
                return;
            }

            byte[] np = outputBitmap.getNinePatchChunk();
            final boolean isNinePatch = np != null && NinePatch.isNinePatchChunk(np);
            if (opts.inScaled || isNinePatch) {
                outputBitmap.setDensity(targetDensity);
            }
        } else if (opts.inBitmap != null) {
            // bitmap was reused, ensure density is reset
            outputBitmap.setDensity(Bitmap.getDefaultDensity());
        }