Reputation: 963
I don't understand why is Bitmap created with Bitmap.createBitmap()
bigger (more kB in native heap), than decoded Bitmap with BitmapFactory.decodeResource()
.
I'm working with LG Optimus One (dpi is 160, Android 2.2) and I'm measuring allocated memory with Debug.getNativeHeapAllocatedSize()
.
If I created Bitmap (200x200) by this way:
Bitmap createdBitmap = Bitmap.createBitmap(200, 200, Config.ARGB_8888);
Then the size of native heap increased by c. 160kB.
But if I decoded PNG image (200x200) from resource (from folder drawable-mdpi
):
Bitmap mdpiBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image_mdpi);
The size increased by c. only 3kB.
But if I decode PNG image (150x150) from drawable-ldpi
, then Android scale up the image to 200x200 and the size of native heap increase also by c. 160kB.
Does someone know, why is created Bitmap and decoded (scaled) Bitmap so big?
Thanks for answer.
Here is code I used to measure:
long createdSizeStart = Debug.getNativeHeapAllocatedSize();
Bitmap createdBitmap = Bitmap.createBitmap(200, 200, Config.ARGB_8888);
long createdSize = Debug.getNativeHeapAllocatedSize() - createdSizeStart;
long resourceMdpiSizeStart = Debug.getNativeHeapAllocatedSize();
Bitmap resourceMdpiBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image_mdpi);
long resourceMdpiSize = Debug.getNativeHeapAllocatedSize() - resourceMdpiSizeStart;
long resourceLdpiSizeStart = Debug.getNativeHeapAllocatedSize();
Bitmap resourceLdpiBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image_ldpi);
long resourceLdpiSize = Debug.getNativeHeapAllocatedSize() - resourceLdpiSizeStart;
Log.i("test", "Created: " + createdSize + " B, " + createdBitmap.getWidth() + "x" + createdBitmap.getHeight());
Log.i("test", "Resource MDPI: " + resourceMdpiSize + " B, " + resourceMdpiBitmap.getWidth() + "x" + resourceMdpiBitmap.getHeight());
Log.i("test", "Resource LDPI: " + resourceLdpiSize + " B, " + resourceLdpiBitmap.getWidth() + "x" + resourceLdpiBitmap.getHeight());
The output is:
Created: 163952 B, 200x200
Resource MDPI: 3184 B, 200x200
Resource LDPI: 164424 B, 200x200
Upvotes: 0
Views: 1946
Reputation: 301
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled=false;
Bitmap bitmap;
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.whatever, options);
Not too sure what your intents are... The code snippet above will create a Bitmap R.drawable.whatever at the actual size... no scaling... same dimensions as it was when you created the resource.
Hope this helps.
Upvotes: 3
Reputation: 13007
"Does someone know, why is created Bitmap and decoded (scaled) Bitmap so big?"
160KB seems correct for 200*200*4 bytes. Your question should be "why is the MDPI bitmap so small?".
I wonder if the MDPI version has been loaded previously by some other piece of code, and this data is being reused.
Upvotes: 1