BitmapFactorypublic 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 |
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();
}
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 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.
*/
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 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.
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 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 decodeFileDescriptor(fd, null, null);
| public static Bitmap | decodeResource(android.content.res.Resources res, int id, android.graphics.BitmapFactory$Options opts)Synonym for opening the given resource and calling
{@link #decodeResourceStream}.
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 Bitmap | decodeResource(android.content.res.Resources res, int id)Synonym for {@link #decodeResource(Resources, int, android.graphics.BitmapFactory.Options)}
with null Options.
return decodeResource(res, id, null);
| public static Bitmap | decodeResourceStream(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 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);
| 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;
}
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 Bitmap | decodeStreamInternal(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 Bitmap | nativeDecodeAsset(long nativeAsset, 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 boolean | nativeIsSeekable(java.io.FileDescriptor fd)
| private static void | setDensityFromOptions(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());
}
|
|