To get the size of bitmap today, use LRUCache to write the demo
So I used it.
return Bitmap.getrowbytes () * bitmap.getheight ();//Get size and return
The number of memory space occupied by bitmap is equal to the number of spaces occupied by each row of bitmap multiplied by the number of bitmap rows
Why not Bitmap.getbytecount ()?
Because GetByteCount requires a high version of the API, consider compatibility using the methods above
1. Getrowbytes:since API Level 1
2. Getbytecount:since API Level 12
View Bitmap Source code
[Java]View PlainCopyprint?
- Public Final int getByteCount () {
- return Getrowbytes () * getheight ();
- }
So after API 12
GetByteCount () = Getrowbytes () * getheight ();
The method above may be helpful when calculating the space occupied by bitmap.
Add:
[Java]View PlainCopyprint?
- /**
- * Get the size of bitmap
- */
- public static int getbitmapsize (Bitmap Bitmap) {
- if (Build.VERSION.SDK_INT >= build.version_codes. KITKAT) { //api
- return Bitmap.getallocationbytecount ();
- }
- if (Build.VERSION.SDK_INT >= build.version_codes. HONEYCOMB_MR1) {//api
- return Bitmap.getbytecount ();
- }
- //byte x height with one row in the lower version
- return Bitmap.getrowbytes () * Bitmap.getheight (); //earlier version
- }
Android Compute Bitmap Size