Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2><strong>1) try to decode the image bound first</strong></h2> <p>you get the actual size of the image to prevent the OOM issue. Dont ever decode the image first!!!</p> <pre><code> BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeByteArray(bitmapByteArray, 0, bitmapByteArray.length, options);' </code></pre> <p>options.outWidth and options.outHeight are what you want.</p> <h2><strong>2) compute a sample size</strong></h2> <p>by using the following code from <a href="http://hi-android.info/src/com/android/camera/Util.java.html" rel="noreferrer">http://hi-android.info/src/com/android/camera/Util.java.html</a>. If you detect the outWidth and outHeight are too big to have OOM issue, just set the outWidth and outHeight to a smaller size. It will give you a sample size that the decoded image will be set to those outWidth and outHeight. The bitmap options here is the same you use in step 1.</p> <pre><code> private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) { double w = options.outWidth; double h = options.outHeight; int lowerBound = (maxNumOfPixels == IImage.UNCONSTRAINED) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels)); int upperBound = (minSideLength == IImage.UNCONSTRAINED) ? 128 : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength)); if (upperBound &lt; lowerBound) { // return the larger one when there is no overlapping zone. return lowerBound; } if ((maxNumOfPixels == IImage.UNCONSTRAINED) &amp;&amp; (minSideLength == IImage.UNCONSTRAINED)) { return 1; } else if (minSideLength == IImage.UNCONSTRAINED) { return lowerBound; } else { return upperBound; } } </code></pre> <h2>3) Use the computed sample size</h2> <p>Once you get the sample size, use it to decode the real image data.</p> <pre><code> options.inTempStorage = new byte[16*1024]; options.inPreferredConfig = (config == null)?BitmapUtil.DEFAULT_CONFIG:config; options.inSampleSize = BitmapUtil.computeSampleSize(bitmapWidth, bitmapHeight, bitmapWidth &lt; bitmapHeight?targetHeight:targetWidth, bitmapWidth &lt; bitmapHeight?targetWidth:targetHeight, 1); options.inPurgeable = true; options.inInputShareable = true; options.inJustDecodeBounds = false; options.inDither = true; result = BitmapFactory.decodeByteArray(bitmapByteArray, 0, bitmapByteArray.length, options); </code></pre> <p>~If this still give you OOM issue, you can try to lower the outWidth and outHeight. bitmap is using native heap, not the java heap. So it is hard to detect how much memory left for the new image decoded.</p> <hr> <p>~~If you have to set the outWidth and outHeight too low, then you may have memory leak somewhere in your code. try to release any object from memory that you dont use. <code>e.g bitmap.release();</code></p> <hr> <p>~~~the above is just a sample code. adjust what you need.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload