I decode bitmaps from an SD card using BitmapFactory.decodeFile . Sometimes bitmaps are larger than what the application requires or what the heap allows, so I use BitmapFactory.Options.inSampleSize to request a subsample of a (smaller) bitmap.
The problem is that the platform does not provide the exact value of inSampleSize, and sometimes I get the bitmap too small or still too large for the available memory.
From http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize :
Note: the decoder will try to fulfill this request, but the received bitmap may have different sizes, which are exactly what was requested. In addition, powers 2 are often faster / easier for an honor decoder.
How do I decode bitmaps from an SD card to get the exact size bitmap that I need, consuming as little memory as possible to decode it?
Edit:
Current Source Code:
BitmapFactory.Options bounds = new BitmapFactory.Options(); this.bounds.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, bounds); if (bounds.outWidth == -1) { // TODO: Error } int width = bounds.outWidth; int height = bounds.outHeight; boolean withinBounds = width <= maxWidth && height <= maxHeight; if (!withinBounds) { int newWidth = calculateNewWidth(int width, int height); float sampleSizeF = (float) width / (float) newWidth; int sampleSize = Math.round(sampleSizeF); BitmapFactory.Options resample = new BitmapFactory.Options(); resample.inSampleSize = sampleSize; bitmap = BitmapFactory.decodeFile(filePath, resample); }
android memory bitmap sd-card
hpique Apr 14 '10 at 23:22 2010-04-14 23:22
source share