BitmapFactorypublic class BitmapFactory extends Object Creates Bitmap objects from various sources, including files, streams,
and byte-arrays. |
Methods Summary |
---|
public static Bitmap | decodeByteArray(byte[] data, int offset, int length, android.graphics.BitmapFactory$Options opts)Decode an immutable bitmap from the specified byte array.
if ((offset | length) < 0 || data.length < offset + length) {
throw new ArrayIndexOutOfBoundsException();
}
return nativeDecodeByteArray(data, offset, length, opts);
| public static Bitmap | decodeByteArray(byte[] data, int offset, int length)Decode an immutable bitmap from the specified byte array.
return decodeByteArray(data, offset, length, null);
| public static Bitmap | decodeFile(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.
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 Bitmap | decodeFile(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.
return decodeFile(pathName, null);
| public static Bitmap | decodeFileDescriptor(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.
return nativeDecodeFileDescriptor(fd, outPadding, opts);
| public static Bitmap | decodeFileDescriptor(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.
return nativeDecodeFileDescriptor(fd, null, null);
| public static Bitmap | decodeResource(android.content.res.Resources res, int id, android.graphics.BitmapFactory$Options opts)Decode an image referenced by a resource ID.
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 Bitmap | decodeResource(android.content.res.Resources res, int id)Decode an image referenced by a resource ID.
return decodeResource(res, id, null);
| public static Bitmap | decodeStream(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();
}
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 Bitmap | decodeStream(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.
// 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 Bitmap | decodeStream(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.
return decodeStream(is, null, null);
| private static native Bitmap | nativeDecodeAsset(int asset, Rect padding, android.graphics.BitmapFactory$Options opts)
| private static native Bitmap | nativeDecodeByteArray(byte[] data, int offset, int length, android.graphics.BitmapFactory$Options opts)
| private static native Bitmap | nativeDecodeFileDescriptor(java.io.FileDescriptor fd, Rect padding, android.graphics.BitmapFactory$Options opts)
| private static native Bitmap | nativeDecodeStream(java.io.InputStream is, byte[] storage, Rect padding, android.graphics.BitmapFactory$Options opts)
| private static native byte[] | nativeScaleNinePatch(byte[] chunk, float scale, Rect pad)
|
|