getRowBytes():每一行所占的空間數。 getByteCount():BitMap的大小。
為什么在一般情況下不用bitmap.getByteCount()呢? 因為getByteCount要求的API版本較高,考慮到兼容性,一般使用上面的getRowBytes方法。
getRowBytes:Since API Level 1
getByteCount:Since API Level 12
源碼:
public final int getByteCount() { return getRowBytes() * getHeight(); }所以,getByteCount()方法也就是實現了一下簡單的封裝。在開發中如果版本有要求可以使用下面代碼,或者直接使用getRowBytes() * getHeight();
/** * 得到bitmap的大小 */ public static int getBitmapSize(Bitmap bitmap) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //API 19 return bitmap.getAllocationByteCount(); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {//API 12 return bitmap.getByteCount(); } // 在低版本中用一行的字節x高度 return bitmap.getRowBytes() * bitmap.getHeight(); //earlier version }新聞熱點
疑難解答