Bitmap详解(下)

一.简介

在 Android 8.0 之前,Bitmap像素占用的内存是在Java Heap中分配的。8.0 及之后,Bitmap像素占用的内存分配到了Native Heap

由于Native Heap的内存分配上限很大,32 位应用的可用内存在 3~4G,64 位上更大,虚拟内存几乎很难耗尽,所以推测 OOM 时 Java heap 中占用内存较多的对象是 Bitmap 成立的情况下,应用更不容易 OOM。

二.compress()方法详解

1.源码

/**
 * Write a compressed version of the bitmap to the specified outputstream.
 * If this returns true, the bitmap can be reconstructed by passing a
 * corresponding inputstream to BitmapFactory.decodeStream(). Note: not
 * all Formats support all bitmap configs directly, so it is possible that
 * the returned bitmap from BitmapFactory could be in a different bitdepth,
 * and/or may have lost per-pixel alpha (e.g. JPEG only supports opaque
 * pixels).
 *
 * @param format   The format of the compressed image
 * @param quality  Hint to the compressor, 0-100. 0 meaning compress for
 *                 small size, 100 meaning compress for max quality. Some
 *                 formats, like PNG which is lossless, will ignore the
 *                 quality setting
 * @param stream   The outputstream to write the compressed data.
 * @return true if successfully compressed to the specified stream.
 */
public boolean compress(CompressFormat format, int quality, OutputStream stream) {
    checkRecycled("Can't compress a recycled bitmap");
    // do explicit check before calling the native method
    if (stream == null) {
        throw new NullPointerException();
    }
    if (quality < 0 || quality > 100) {
        throw new IllegalArgumentException("quality must be 0..100");
    }
    StrictMode.noteSlowCall("Compression of a bitmap is slow");
    Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, "Bitmap.compress");
    boolean result = nativeCompress(mNativePtr, format.nativeInt,
            quality, stream, new byte[WORKING_COMPRESS_STORAGE]);
    Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
    return result;
}

最终调用native方法

private static native boolean nativeCompress(long nativeBitmap, int format,
                                            int quality, OutputStream stream,
                                            byte[] tempStorage);


2.使用

/**
 * 质量压缩图片
 */

private Bitmap qualityBitmap(Bitmap bitmap) {
    if (null == bitmap || bitmap.isRecycled()) return null;
    try {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        boolean isSuccess = bitmap.compress(Bitmap.CompressFormat.JPEG, 80, byteArrayOutputStream);
        if (isSuccess) {//压缩成功
            byte[] bitmapByte = byteArrayOutputStream.toByteArray();
            return BitmapFactory.decodeByteArray(bitmapByte, 0, bitmapByte.length);
        }
    } catch (Exception ignored) {

    }
    return null;
}

3.总结

由以上源码及使用详解,可知compress()方法有以下几个总结点

<1> compress()方法有返回值 

If this returns true, the bitmap can be reconstructed by passing a corresponding inputstream to BitmapFactory.decodeStream().

即 返回true时代表压缩成功,可以通过ByteArrayOutputStream对象获取Bitmap对象

<2> compress()方法入参

参数1:CompressFormat format

format是一个枚举类型

 参数2:int quality quality取值范围【0-100】只有参数1format入参为JPEG时,此参数才有意义

Hint to the compressor, 0-100.  0 meaning compress for small size, 100 meaning compress for max quality.  Some formats, like PNG which is lossless, will ignore the quality setting



压缩机提示,0-100。0表示压缩小尺寸,100表示压缩最大质量。有些格式,比如PNG是无损的,会忽略质量设置

<3> 由源码注释可知,compress()方法有损压缩&质量压缩不会对内存产生影响

三.BitmapFactory内部静态类Options详解

源码

public static class Options {
    /**
     * Create a default Options object, which if left unchanged will give
     * the same result from the decoder as if null were passed.
     */
    public Options() {
        inScaled = true;
        inPremultiplied = true;
    }

    /**
     * If set, decode methods that take the Options object will attempt to
     * reuse this bitmap when loading content. If the decode operation
     * cannot use this bitmap, the decode method will throw an
     * {@link java.lang.IllegalArgumentException}. The
     * current implementation necessitates that the reused bitmap be
     * mutable, and the resulting reused bitmap will continue to remain
     * mutable even when decoding a resource which would normally result in
     * an immutable bitmap.</p>
     *
     * <p>You should still always use the returned Bitmap of the decode
     * method and not assume that reusing the bitmap worked, due to the
     * constraints outlined above and failure situations that can occur.
     * Checking whether the return value matches the value of the inBitmap
     * set in the Options structure will indicate if the bitmap was reused,
     * but in all cases you should use the Bitmap returned by the decoding
     * function to ensure that you are using the bitmap that was used as the
     * decode destination.</p>
     *
     * <h3>Usage with BitmapFactory</h3>
         *
     * <p>As of {@link android.os.Build.VERSION_CODES#KITKAT}, any
     * mutable bitmap can be reused by {@link BitmapFactory} to decode any
     * other bitmaps as long as the resulting {@link Bitmap#getByteCount()
     * byte count} of the decoded bitmap is less than or equal to the {@link
     * Bitmap#getAllocationByteCount() allocated byte count} of the reused
     * bitmap. This can be because the intrinsic size is smaller, or its
     * size post scaling (for density / sample size) is smaller.</p>
     *
     * <p class="note">Prior to {@link android.os.Build.VERSION_CODES#KITKAT}
     * additional constraints apply: The image being decoded (whether as a
     * resource or as a stream) must be in jpeg or png format. Only equal
     * sized bitmaps are supported, with {@link #inSampleSize} set to 1.
     * Additionally, the {@link android.graphics.Bitmap.Config
     * configuration} of the reused bitmap will override the setting of
     * {@link #inPreferredConfig}, if set.</p>
     *
     * <h3>Usage with BitmapRegionDecoder</h3>
     *
     * <p>BitmapRegionDecoder will draw its requested content into the Bitmap
     * provided, clipping if the output content size (post scaling) is larger
     * than the provided Bitmap. The provided Bitmap's width, height, and
     * {@link Bitmap.Config} will not be changed.
     *
     * <p class="note">BitmapRegionDecoder support for {@link #inBitmap} was
     * introduced in {@link android.os.Build.VERSION_CODES#JELLY_BEAN}. All
     * formats supported by BitmapRegionDecoder support Bitmap reuse via
     * {@link #inBitmap}.</p>
     *
     * @see Bitmap#reconfigure(int,int, android.graphics.Bitmap.Config)
     */
    public Bitmap inBitmap;

    /**
     * If set, decode methods will always return a mutable Bitmap instead of
     * an immutable one. This can be used for instance to programmatically apply
     * effects to a Bitmap loaded through BitmapFactory.
     * <p>Can not be set simultaneously with inPreferredConfig =
     * {@link android.graphics.Bitmap.Config#HARDWARE},
     * because hardware bitmaps are always immutable.
     */
    @SuppressWarnings({"UnusedDeclaration"}) // used in native code
    public boolean inMutable;

    /**
     * If set to true, the decoder will return null (no bitmap), but
     * the <code>out...</code> fields will still be set, allowing the caller to
     * query the bitmap without having to allocate the memory for its pixels.
     */
    public boolean inJustDecodeBounds;

    /**
     * If set to a value > 1, requests the decoder to subsample the original
     * image, returning a smaller image to save memory. The sample size is
     * the number of pixels in either dimension that correspond to a single
     * pixel in the decoded bitmap. For example, inSampleSize == 4 returns
     * an image that is 1/4 the width/height of the original, and 1/16 the
     * number of pixels. Any value <= 1 is treated the same as 1. Note: the
     * decoder uses a final value based on powers of 2, any other value will
     * be rounded down to the nearest power of 2.
     */
    public int inSampleSize;

    /**
     * If this is non-null, the decoder will try to decode into this
     * internal configuration. If it is null, or the request cannot be met,
     * the decoder will try to pick the best matching config based on the
     * system's screen depth, and characteristics of the original image such
     * as if it has per-pixel alpha (requiring a config that also does).
     * 
     * Image are loaded with the {@link Bitmap.Config#ARGB_8888} config by
     * default.
     */
    public Bitmap.Config inPreferredConfig = Bitmap.Config.ARGB_8888;

    /**
     * <p>If this is non-null, the decoder will try to decode into this
     * color space. If it is null, or the request cannot be met,
     * the decoder will pick either the color space embedded in the image
     * or the color space best suited for the requested image configuration
     * (for instance {@link ColorSpace.Named#SRGB sRGB} for
     * the {@link Bitmap.Config#ARGB_8888} configuration).</p>
     *
     * <p>{@link Bitmap.Config#RGBA_F16} always uses the
     * {@link ColorSpace.Named#LINEAR_EXTENDED_SRGB scRGB} color space).
     * Bitmaps in other configurations without an embedded color space are
     * assumed to be in the {@link ColorSpace.Named#SRGB sRGB} color space.</p>
     *
     * <p class="note">Only {@link ColorSpace.Model#RGB} color spaces are
     * currently supported. An <code>IllegalArgumentException</code> will
     * be thrown by the decode methods when setting a non-RGB color space
     * such as {@link ColorSpace.Named#CIE_LAB Lab}.</p>
     *
     * <p class="note">The specified color space's transfer function must be
     * an {@link ColorSpace.Rgb.TransferParameters ICC parametric curve}. An
     * <code>IllegalArgumentException</code> will be thrown by the decode methods
     * if calling {@link ColorSpace.Rgb#getTransferParameters()} on the
     * specified color space returns null.</p>
     *
     * <p>After decode, the bitmap's color space is stored in
     * {@link #outColorSpace}.</p>
     */
    public ColorSpace inPreferredColorSpace = null;

    /**
     * If true (which is the default), the resulting bitmap will have its
     * color channels pre-multipled by the alpha channel.
     *
     * <p>This should NOT be set to false for images to be directly drawn by
     * the view system or through a {@link Canvas}. The view system and
     * {@link Canvas} assume all drawn images are pre-multiplied to simplify
     * draw-time blending, and will throw a RuntimeException when
     * un-premultiplied are drawn.</p>
     *
     * <p>This is likely only useful if you want to manipulate raw encoded
     * image data, e.g. with RenderScript or custom OpenGL.</p>
     *
     * <p>This does not affect bitmaps without an alpha channel.</p>
     *
     * <p>Setting this flag to false while setting {@link #inScaled} to true
     * may result in incorrect colors.</p>
     *
     * @see Bitmap#hasAlpha()
     * @see Bitmap#isPremultiplied()
     * @see #inScaled
     */
    public boolean inPremultiplied;

    /**
     * @deprecated As of {@link android.os.Build.VERSION_CODES#N}, this is
     * ignored.
     *
     * In {@link android.os.Build.VERSION_CODES#M} and below, if dither is
     * true, the decoder will attempt to dither the decoded image.
     */
    public boolean inDither;

    /**
     * The pixel density to use for the bitmap.  This will always result
     * in the returned bitmap having a density set for it (see
     * {@link Bitmap#setDensity(int) Bitmap.setDensity(int)}).  In addition,
     * if {@link #inScaled} is set (which it is by default} and this
     * density does not match {@link #inTargetDensity}, then the bitmap
     * will be scaled to the target density before being returned.
     * 
     * <p>If this is 0,
     * {@link BitmapFactory#decodeResource(Resources, int)}, 
     * {@link BitmapFactory#decodeResource(Resources, int, android.graphics.BitmapFactory.Options)},
     * and {@link BitmapFactory#decodeResourceStream}
     * will fill in the density associated with the resource.  The other
     * functions will leave it as-is and no density will be applied.
     *
     * @see #inTargetDensity
     * @see #inScreenDensity
     * @see #inScaled
     * @see Bitmap#setDensity(int)
     * @see android.util.DisplayMetrics#densityDpi
     */
    public int inDensity;

    /**
     * The pixel density of the destination this bitmap will be drawn to.
     * This is used in conjunction with {@link #inDensity} and
     * {@link #inScaled} to determine if and how to scale the bitmap before
     * returning it.
     * 
     * <p>If this is 0,
     * {@link BitmapFactory#decodeResource(Resources, int)}, 
     * {@link BitmapFactory#decodeResource(Resources, int, android.graphics.BitmapFactory.Options)},
     * and {@link BitmapFactory#decodeResourceStream}
     * will fill in the density associated the Resources object's
     * DisplayMetrics.  The other
     * functions will leave it as-is and no scaling for density will be
     * performed.
     * 
     * @see #inDensity
     * @see #inScreenDensity
     * @see #inScaled
     * @see android.util.DisplayMetrics#densityDpi
     */
    public int inTargetDensity;
        
    /**
     * The pixel density of the actual screen that is being used.  This is
     * purely for applications running in density compatibility code, where
     * {@link #inTargetDensity} is actually the density the application
     * sees rather than the real screen density.
     * 
     * <p>By setting this, you
     * allow the loading code to avoid scaling a bitmap that is currently
     * in the screen density up/down to the compatibility density.  Instead,
     * if {@link #inDensity} is the same as {@link #inScreenDensity}, the
     * bitmap will be left as-is.  Anything using the resulting bitmap
     * must also used {@link Bitmap#getScaledWidth(int)
     * Bitmap.getScaledWidth} and {@link Bitmap#getScaledHeight
     * Bitmap.getScaledHeight} to account for any different between the
     * bitmap's density and the target's density.
     * 
     * <p>This is never set automatically for the caller by
     * {@link BitmapFactory} itself.  It must be explicitly set, since the
     * caller must deal with the resulting bitmap in a density-aware way.
     * 
     * @see #inDensity
     * @see #inTargetDensity
     * @see #inScaled
     * @see android.util.DisplayMetrics#densityDpi
     */
    public int inScreenDensity;
        
    /**
     * When this flag is set, if {@link #inDensity} and
     * {@link #inTargetDensity} are not 0, the
     * bitmap will be scaled to match {@link #inTargetDensity} when loaded,
     * rather than relying on the graphics system scaling it each time it
     * is drawn to a Canvas.
     *
     * <p>BitmapRegionDecoder ignores this flag, and will not scale output
     * based on density. (though {@link #inSampleSize} is supported)</p>
     *
     * <p>This flag is turned on by default and should be turned off if you need
     * a non-scaled version of the bitmap.  Nine-patch bitmaps ignore this
     * flag and are always scaled.
     *
     * <p>If {@link #inPremultiplied} is set to false, and the image has alpha,
     * setting this flag to true may result in incorrect colors.
     */
    public boolean inScaled;

    /**
     * @deprecated As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this is
     * ignored.
     *
     * In {@link android.os.Build.VERSION_CODES#KITKAT} and below, if this
     * is set to true, then the resulting bitmap will allocate its
     * pixels such that they can be purged if the system needs to reclaim
     * memory. In that instance, when the pixels need to be accessed again
     * (e.g. the bitmap is drawn, getPixels() is called), they will be
     * automatically re-decoded.
     *
     * <p>For the re-decode to happen, the bitmap must have access to the
     * encoded data, either by sharing a reference to the input
     * or by making a copy of it. This distinction is controlled by
     * inInputShareable. If this is true, then the bitmap may keep a shallow
     * reference to the input. If this is false, then the bitmap will
     * explicitly make a copy of the input data, and keep that. Even if
     * sharing is allowed, the implementation may still decide to make a
     * deep copy of the input data.</p>
     *
     * <p>While inPurgeable can help avoid big Dalvik heap allocations (from
     * API level 11 onward), it sacrifices performance predictability since any
     * image that the view system tries to draw may incur a decode delay which
     * can lead to dropped frames. Therefore, most apps should avoid using
     * inPurgeable to allow for a fast and fluid UI. To minimize Dalvik heap
     * allocations use the {@link #inBitmap} flag instead.</p>
     *
     * <p class="note"><strong>Note:</strong> This flag is ignored when used
     * with {@link #decodeResource(Resources, int,
     * android.graphics.BitmapFactory.Options)} or {@link #decodeFile(String,
     * android.graphics.BitmapFactory.Options)}.</p>
     */
    @Deprecated
    public boolean inPurgeable;

    /**
     * @deprecated As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this is
     * ignored.
     *
     * In {@link android.os.Build.VERSION_CODES#KITKAT} and below, this
     * field works in conjuction with inPurgeable. If inPurgeable is false,
     * then this field is ignored. If inPurgeable is true, then this field
     * determines whether the bitmap can share a reference to the input
     * data (inputstream, array, etc.) or if it must make a deep copy.
     */
    @Deprecated
    public boolean inInputShareable;

    /**
     * @deprecated As of {@link android.os.Build.VERSION_CODES#N}, this is
     * ignored.  The output will always be high quality.
     *
     * In {@link android.os.Build.VERSION_CODES#M} and below, if
     * inPreferQualityOverSpeed is set to true, the decoder will try to
     * decode the reconstructed image to a higher quality even at the
     * expense of the decoding speed. Currently the field only affects JPEG
     * decode, in the case of which a more accurate, but slightly slower,
     * IDCT method will be used instead.
     */
    public boolean inPreferQualityOverSpeed;

    /**
     * The resulting width of the bitmap. If {@link #inJustDecodeBounds} is
     * set to false, this will be width of the output bitmap after any
     * scaling is applied. If true, it will be the width of the input image
     * without any accounting for scaling.
     *
     * <p>outWidth will be set to -1 if there is an error trying to decode.</p>
     */
    public int outWidth;

    /**
     * The resulting height of the bitmap. If {@link #inJustDecodeBounds} is
     * set to false, this will be height of the output bitmap after any
     * scaling is applied. If true, it will be the height of the input image
     * without any accounting for scaling.
     *
     * <p>outHeight will be set to -1 if there is an error trying to decode.</p>
     */
    public int outHeight;

    /**
     * If known, this string is set to the mimetype of the decoded image.
     * If not known, or there is an error, it is set to null.
     */
    public String outMimeType;

    /**
     * If known, the config the decoded bitmap will have.
     * If not known, or there is an error, it is set to null.
     */
    public Bitmap.Config outConfig;

    /**
     * If known, the color space the decoded bitmap will have. Note that the
     * output color space is not guaranteed to be the color space the bitmap
     * is encoded with. If not known (when the config is
     * {@link Bitmap.Config#ALPHA_8} for instance), or there is an error,
     * it is set to null.
     */
    public ColorSpace outColorSpace;

    /**
     * Temp storage to use for decoding.  Suggest 16K or so.
     */
    public byte[] inTempStorage;

    /**
     * @deprecated As of {@link android.os.Build.VERSION_CODES#N}, see
     * comments on {@link #requestCancelDecode()}.
     *
     * Flag to indicate that cancel has been called on this object.  This
     * is useful if there's an intermediary that wants to first decode the
     * bounds and then decode the image.  In that case the intermediary
     * can check, inbetween the bounds decode and the image decode, to see
     * if the operation is canceled.
     */
    public boolean mCancel;

    /**
     *  @deprecated As of {@link android.os.Build.VERSION_CODES#N}, this
     *  will not affect the decode, though it will still set mCancel.
     *
     *  In {@link android.os.Build.VERSION_CODES#M} and below, if this can
     *  be called from another thread while this options object is inside
     *  a decode... call. Calling this will notify the decoder that it
     *  should cancel its operation. This is not guaranteed to cancel the
     *  decode, but if it does, the decoder... operation will return null,
     *  or if inJustDecodeBounds is true, will set outWidth/outHeight
     *  to -1
     */
    public void requestCancelDecode() {
        mCancel = true;
    }

    static void validate(Options opts) {
        if (opts == null) return;

        if (opts.inBitmap != null && opts.inBitmap.getConfig() == Bitmap.Config.HARDWARE) {
            throw new IllegalArgumentException("Bitmaps with Config.HARWARE are always immutable");
        }

        if (opts.inMutable && opts.inPreferredConfig == Bitmap.Config.HARDWARE) {
            throw new IllegalArgumentException("Bitmaps with Config.HARDWARE cannot be " +
                    "decoded into - they are immutable");
        }

        if (opts.inPreferredColorSpace != null) {
            if (!(opts.inPreferredColorSpace instanceof ColorSpace.Rgb)) {
                throw new IllegalArgumentException("The destination color space must use the " +
                        "RGB color model");
            }
            if (((ColorSpace.Rgb) opts.inPreferredColorSpace).getTransferParameters() == null) {
                throw new IllegalArgumentException("The destination color space must use an " +
                        "ICC parametric transfer function");
            }
        }
    }
}
1.内部静态类Options属性之inSampleSize
说明1
inSampleSize小于1的话会被当做1
Any value <= 1 is treated the same as 1

说明2

inSampleSize大于1时,Bitmap的宽&高都会缩小inSampleSize倍。

For example, inSampleSize == 4

returns an image that is 1/4 the width/height of the original 

and 1/16 the number of pixels

说明3

inSampleSize值的大小不是随便设、或者越大越好,任何inSampleSize的值会被取接近2的幂值

the decoder uses a final value based on powers of 2, 

any other value will be rounded down to the nearest power of 2.

2.内部静态类Options属性之inJustDecodeBounds
说明
将 inJustDecodeBounds 属性设置为 true,可以在解码时避免内存的分配,它会返回一个 null 的 Bitmap ,但是可以获取 outWidth、outHeight 和 outMimeType 值。利用该属性,我们就可以在图片不占用内存的情况下,在图片压缩之前获取图片的尺寸
If set to true, the decoder will return null (no bitmap), 

but the out...  fields will still be set, 

allowing the caller to query the bitmap without having to allocate the memory for its pixels.

3.内部静态类Options属性之inMutable

说明1

inMutable可设置Bitmap是可变的,一般与InBitmap属性一同使用,用来使Bitmap内存复用。

If set, decode methods will always return 

a mutable Bitmap instead of an immutable one.

说明2

inMutable不可在使用Bitmap的Config枚举为HARDWARE一起使用。

Can not be set simultaneously with inPreferredConfig 

because hardware bitmaps are always immutable.

4.内部静态类Options属性之inBitmap

说明1

如果设置inBitmap,则采用Options对象的decode方法将在加载内容时尝试重用此位图。

If set, decode methods that take the Options object will 

attempt to reuse this bitmap when loading content

说明2

此属性需要和inMutable属性一起使用

The current implementation necessitates that the reused bitmap be mutable

说明3

如果,此属性使用不当可能会抛出异常

If the decode operation cannot use this bitmap,

the decode method will throw an {@link java.lang.IllegalArgumentException}

5.属性inSampleSize&属性inJustDecodeBounds应用举例

<1> 代码

/**
 * Bitmap裁剪
 *
 * @param path   图片本地路径
 * @param width  最大宽度
 * @param height 最大高度
 */

public static Bitmap decodeBitmap(String path, int width, int height) {
    if (StringUtils.isEmpty(path) || width <= 0 || height <= 0) return null;

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;

    BitmapFactory.decodeFile(path, options);

    options.inJustDecodeBounds = false;

    options.inSampleSize = calculateInSampleSize(options, width, height);
    Bitmap result = BitmapFactory.decodeFile(path, options);
    
    return result;
}

<2> 说明

(1).将BitmapFactory.Options的inJustDecodeBounds属性设置为true,解析一次图片。这个解析图片并没有生成bitmap对象(也就是说没有为它分配内存控件),而仅仅是拿到它的宽高等属性

(2).然后将BitmapFactory.Options连同期望的宽度和高度一起传递到到calculateInSampleSize方法中,就可以得到合适的inSampleSize值了。这一步会压缩图片。

(3).之后再解析一次图片,使用新获取到的inSampleSize值,并把inJustDecodeBounds设置为false,就可以得到压缩后的图片了。此时才正式创建了bitmap对象
 

6.属性inSampleSize&属性inMutable&属性inBitmap应用举例

<1> 代码

private void initBitmap() {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inMutable = true;//inMutable属性设置true Bitmap可变的
    Bitmap firstBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.banner_default, options);
    LoggerUtils.logD(TAG, "BitmapFactory 设置 firstBitmap 内存地址:" + firstBitmap);
    LoggerUtils.logD(TAG, "BitmapFactory 设置 firstBitmap getByteCount:" + firstBitmap.getByteCount());
    LoggerUtils.logD(TAG, "BitmapFactory 设置 firstBitmap getAllocationByteCount:" + firstBitmap.getAllocationByteCount());

    options.inBitmap = firstBitmap;//inBitmap属性 Bitmap复用
    options.inSampleSize = 2;//inSampleSize属性 设置新Bitmap宽&高为原来的1/2
    Bitmap reuseBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.banner_default, options);
    LoggerUtils.logD(TAG, "BitmapFactory 设置 reuseBitmap 内存地址:" + reuseBitmap);
    LoggerUtils.logD(TAG, "BitmapFactory 设置 reuseBitmap:getByteCount:" + reuseBitmap.getByteCount());
    LoggerUtils.logD(TAG, "BitmapFactory 设置 reuseBitmap:getAllocationByteCount:" + reuseBitmap.getAllocationByteCount());
}

<2> 结果

设置 firstBitmap 内存地址:android.graphics.Bitmap@fd9736b
设置 firstBitmap getByteCount:377720
设置 firstBitmap getAllocationByteCount:377720


设置 reuseBitmap 内存地址:android.graphics.Bitmap@fd9736b
设置 reuseBitmap:getByteCount:94164
设置 reuseBitmap:getAllocationByteCount:377720

<3> 说明

(1) 前后两个Bitmap的内存地址一样,说明内存是复用的。

(2) 设置了Options属性后,getByteCount()方法和getAllocationByteCount()方法返回的大小确实不一样。

getByteCount()方法:返回可用于存储此位图像素的最小字节数。

getAllocationByteCount()方法:返回存储此位图像素的已分配内存的大小。

此段代码复用后的Bitmap宽度都变成原来的1/2,所以所占内存就会变成原来的1/4。

377720除以4约等于94164。

<4> 总结

Android4.4(API 19)之后被复用的Bitmap内存必须大于需要申请内存的Bitmap的内存

也就是上面刚刚提到的inBitmap属性,需要复用的Bitmap是原始(被复用)Bitmap的宽高1/2,这种情况下,使用inBitmap就没有问题。代码及结果如上。

但是,如果需要复用的Bitmap是原始(被复用)Bitmap的宽高的2倍,这种情况下,使用inBitmap就会有问题,报错如下

E/DEBUG: java.lang.RuntimeException: Unable to resume activity {onemap.guojiao.com.gjonemap.dev/onemap.guojiao.com.gjonemap.main.ui.MainActivity}: java.lang.IllegalArgumentException: Problem decoding into existing bitmap
E/DEBUG:     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3627)
E/DEBUG:     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3667)
E/DEBUG:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2901)
E/DEBUG:     at android.app.ActivityThread.-wrap11(Unknown Source:0)
E/DEBUG:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1616)
E/DEBUG:     at android.os.Handler.dispatchMessage(Handler.java:106)
E/DEBUG:     at android.os.Looper.loop(Looper.java:176)
E/DEBUG:     at android.app.ActivityThread.main(ActivityThread.java:6651)
E/DEBUG:     at java.lang.reflect.Method.invoke(Native Method)
E/DEBUG:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
E/DEBUG:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
E/DEBUG: Caused by: java.lang.IllegalArgumentException: Problem decoding into existing bitmap
E/DEBUG:     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:582)
E/DEBUG:     at onemap.guojiao.com.gjonemap.main.ui.MainActivity.h1(MainActivity.java:10)
E/DEBUG:     at onemap.guojiao.com.gjonemap.main.ui.MainActivity.z0(MainActivity.java:5)
E/DEBUG:     at com.guojiao.ui.ui.activity.BaseActivity.onResume(BaseActivity.java:3)
E/DEBUG:     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1356)
E/DEBUG:     at android.app.Activity.performResume(Activity.java:7196)
E/DEBUG:     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3602)
E/DEBUG: 	... 10 more

报错关键字:Problem decoding into existing bitmap。

报错位置:BitmapFactory类

报错代码:

/**
 * 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)
 * @throws IllegalArgumentException if {@link BitmapFactory.Options#inPreferredConfig}
 *         is {@link android.graphics.Bitmap.Config#HARDWARE}
 *         and {@link BitmapFactory.Options#inMutable} is set, if the specified color space
 *         is not {@link ColorSpace.Model#RGB RGB}, or if the specified color space's transfer
 *         function is not an {@link ColorSpace.Rgb.TransferParameters ICC parametric curve}
 */
public static Bitmap decodeResource(Resources res, int id, Options opts) {
    validate(opts);
    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;
}
/**
 * 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)
 * @throws IllegalArgumentException if {@link BitmapFactory.Options#inPreferredConfig}
 *         is {@link android.graphics.Bitmap.Config#HARDWARE}
 *         and {@link BitmapFactory.Options#inMutable} is set, if the specified color space
 *         is not {@link ColorSpace.Model#RGB RGB}, or if the specified color space's transfer
 *         function is not an {@link ColorSpace.Rgb.TransferParameters ICC parametric curve}
 */
public static Bitmap decodeByteArray(byte[] data, int offset, int length, Options opts) {
    if ((offset | length) < 0 || data.length < offset + length) {
        throw new ArrayIndexOutOfBoundsException();
    }
    validate(opts);

    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;
}
/**
 * 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)
 * @throws IllegalArgumentException if {@link BitmapFactory.Options#inPreferredConfig}
 *         is {@link android.graphics.Bitmap.Config#HARDWARE}
 *         and {@link BitmapFactory.Options#inMutable} is set, if the specified color space
 *         is not {@link ColorSpace.Model#RGB RGB}, or if the specified color space's transfer
 *         function is not an {@link ColorSpace.Rgb.TransferParameters ICC parametric curve}
 *
 * <p class="note">Prior to {@link android.os.Build.VERSION_CODES#KITKAT},
 * if {@link InputStream#markSupported is.markSupported()} returns true,
 * <code>is.mark(1024)</code> would be called. As of
 * {@link android.os.Build.VERSION_CODES#KITKAT}, this is no longer the case.</p>
 */
public static Bitmap decodeStream(InputStream is, Rect outPadding, Options opts) {
    // 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;
    }
    validate(opts);

    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;
}
/**
 * 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
 * @throws IllegalArgumentException if {@link BitmapFactory.Options#inPreferredConfig}
 *         is {@link android.graphics.Bitmap.Config#HARDWARE}
 *         and {@link BitmapFactory.Options#inMutable} is set, if the specified color space
 *         is not {@link ColorSpace.Model#RGB RGB}, or if the specified color space's transfer
 *         function is not an {@link ColorSpace.Rgb.TransferParameters ICC parametric curve}
*/
public static Bitmap decodeFileDescriptor(FileDescriptor fd, Rect outPadding, Options opts) {
    validate(opts);
    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;
}

即,此错误报错的位置都是BitmapFactory类的decodeXXX重载方法。

四.BitmapRegionDecoder详解

1.简介

当一张图片非常大,在手机中只需要显示其中一部分内容时,BitmapRegionDecoder就会非常有用途了。BitmapRegionDecoder 可以从图像中解码一个矩形区域 。相当于手在滑动的过程中,计算当前显示区域的图片绘制出来。

2.代码

BitmapRegionDecoder bitmapRegionDecoder = null;
try {
    bitmapRegionDecoder = BitmapRegionDecoder.newInstance(is, false);
} catch (IOException e) {
    e.printStackTrace();
}

if (null != bitmapRegionDecoder) {
    //加载整个图像
    Rect allRect = new Rect(0, 0, bitmapRegionDecoder.getWidth(), bitmapRegionDecoder.getHeight());
    Bitmap allBitmap = bitmapRegionDecoder.decodeRegion(allRect, null);

    //加载图像的一部分
    Rect partRect = new Rect(50, 50, 300, 300);
    Bitmap partBitmap = bitmapRegionDecoder.decodeRegion(partRect, null);

    if (!bitmapRegionDecoder.isRecycled()) bitmapRegionDecoder.recycle();
}

3.说明

<1> 获取BitmapRegionDecoder对象使用BitmapRegionDecoder.newInstance()方法。BitmapRegionDecoder.newInstance()方法有几个重载。

/**
 * Create a BitmapRegionDecoder from the specified byte array.
 * Currently only the JPEG and PNG formats are supported.
 *
 * @param data byte array of compressed image data.
 * @param offset offset into data for where the decoder should begin
 *               parsing.
 * @param length the number of bytes, beginning at offset, to parse
 * @param isShareable If this is true, then the BitmapRegionDecoder may keep a
 *                    shallow reference to the input. If this is false,
 *                    then the BitmapRegionDecoder will explicitly make a copy of the
 *                    input data, and keep that. Even if sharing is allowed,
 *                    the implementation may still decide to make a deep
 *                    copy of the input data. If an image is progressively encoded,
 *                    allowing sharing may degrade the decoding speed.
 * @return BitmapRegionDecoder, or null if the image data could not be decoded.
 * @throws IOException if the image format is not supported or can not be decoded.
 */
public static BitmapRegionDecoder newInstance(byte[] data,
        int offset, int length, boolean isShareable) throws IOException {
    if ((offset | length) < 0 || data.length < offset + length) {
        throw new ArrayIndexOutOfBoundsException();
    }
    return nativeNewInstance(data, offset, length, isShareable);
}

/**
 * Create a BitmapRegionDecoder from the file descriptor.
 * The position within the descriptor will not be changed when
 * this returns, so the descriptor can be used again as is.
 * Currently only the JPEG and PNG formats are supported.
 *
 * @param fd The file descriptor containing the data to decode
 * @param isShareable If this is true, then the BitmapRegionDecoder may keep a
 *                    shallow reference to the input. If this is false,
 *                    then the BitmapRegionDecoder will explicitly make a copy of the
 *                    input data, and keep that. Even if sharing is allowed,
 *                    the implementation may still decide to make a deep
 *                    copy of the input data. If an image is progressively encoded,
 *                    allowing sharing may degrade the decoding speed.
 * @return BitmapRegionDecoder, or null if the image data could not be decoded.
 * @throws IOException if the image format is not supported or can not be decoded.
 */
public static BitmapRegionDecoder newInstance(
        FileDescriptor fd, boolean isShareable) throws IOException {
    return nativeNewInstance(fd, isShareable);
}

/**
 * Create a BitmapRegionDecoder from an input stream.
 * The stream's position will be where ever it was after the encoded data
 * was read.
 * Currently only the JPEG and PNG formats are supported.
 *
 * @param is The input stream that holds the raw data to be decoded into a
 *           BitmapRegionDecoder.
 * @param isShareable If this is true, then the BitmapRegionDecoder may keep a
 *                    shallow reference to the input. If this is false,
 *                    then the BitmapRegionDecoder will explicitly make a copy of the
 *                    input data, and keep that. Even if sharing is allowed,
 *                    the implementation may still decide to make a deep
 *                    copy of the input data. If an image is progressively encoded,
 *                    allowing sharing may degrade the decoding speed.
 * @return BitmapRegionDecoder, or null if the image data could not be decoded.
 * @throws IOException if the image format is not supported or can not be decoded.
 *
 * <p class="note">Prior to {@link android.os.Build.VERSION_CODES#KITKAT},
 * if {@link InputStream#markSupported is.markSupported()} returns true,
 * <code>is.mark(1024)</code> would be called. As of
 * {@link android.os.Build.VERSION_CODES#KITKAT}, this is no longer the case.</p>
 */
public static BitmapRegionDecoder newInstance(InputStream is,
        boolean isShareable) throws IOException {
    if (is instanceof AssetManager.AssetInputStream) {
        return nativeNewInstance(
                ((AssetManager.AssetInputStream) is).getNativeAsset(),
                isShareable);
    } 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(...).
        byte [] tempStorage = new byte[16 * 1024];
        return nativeNewInstance(is, tempStorage, isShareable);
    }
}

/**
 * Create a BitmapRegionDecoder from a file path.
 * Currently only the JPEG and PNG formats are supported.
 *
 * @param pathName complete path name for the file to be decoded.
 * @param isShareable If this is true, then the BitmapRegionDecoder may keep a
 *                    shallow reference to the input. If this is false,
 *                    then the BitmapRegionDecoder will explicitly make a copy of the
 *                    input data, and keep that. Even if sharing is allowed,
 *                    the implementation may still decide to make a deep
 *                    copy of the input data. If an image is progressively encoded,
 *                    allowing sharing may degrade the decoding speed.
 * @return BitmapRegionDecoder, or null if the image data could not be decoded.
 * @throws IOException if the image format is not supported or can not be decoded.
 */
public static BitmapRegionDecoder newInstance(String pathName,
        boolean isShareable) throws IOException {
    BitmapRegionDecoder decoder = null;
    InputStream stream = null;

    try {
        stream = new FileInputStream(pathName);
        decoder = newInstance(stream, isShareable);
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                // do nothing here
            }
        }
    }
    return decoder;
}

<2> decodeRegion方获取Rect(矩形)对象,来确定图片的区域。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值