2011-06-21 26 views
10

我知道Android平臺是一個巨大的混亂,過於複雜和過度設計,但認真獲取位圖的大小,是否真的有必要做所有這些轉換?heck是位圖getByteCount()?

Bitmap bitmap = your bitmap object 
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
    byte[] imageInByte = stream.toByteArray(); 
long length = imageInByte.length; 

據谷歌文檔位圖有一個方法getByteCount()要做到這一點,但它是不存在的SDK2.2,還沒有嘗試過其他的,但沒有它被廢棄提或API的支持是任何不同於API 1 ...那麼這種神祕的方法隱藏在哪裏?這將真正高興能ALBE簡單做

bitmap.getByteCount() 

回答

12

如果通過API等級8(= 2.2 SDK)過濾器,你會看到Bitmap#getByteCount()是灰色的,這意味着法中不存在該API級別。

getByteCount()在API級別12

+0

的SDK現在可以幫助您檢測您的項目中舊版(目標版)中不存在的API。 –

61

我剛纔寫的方法添加。 AndroidVersion.java是我創建的一個類,可以很容易地從手機中獲取版本代碼。

http://code.google.com/p/android-beryl/source/browse/beryl/src/org/beryl/app/AndroidVersion.java

public static long getSizeInBytes(Bitmap bitmap) { 
    if(AndroidVersion.isHoneycombMr2OrHigher()) { 
     return bitmap.getByteCount(); 
    } else { 
     return bitmap.getRowBytes() * bitmap.getHeight(); 
    } 
} 
+18

getByteCount()只是一個方便的方法,它完全符合你放入else塊的內容。換句話說,如果您只是簡單地重寫getSizeInBytes以始終返回「bitmap.getRowBytes()* bitmap.getHeight()」,則不需要Android版本檢查。 –

+1

你應該返回'(bitmap.getRowBytes()* bitmap.getHeight())/ 1024;' –

1

API 12之前就可以計算出使用位圖的字節大小的getHeight()*的getWidth()* 4如果正在使用ARGB_8888因爲每一個像素被存儲在4字節。我認爲這是默認格式。

1

正如其他答案中所提到的,它僅適用於API 12或更高版本。這是該方法的簡單兼容版本。

public static int getByteCount(Bitmap bitmap) { 
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) { 
     return bitmap.getRowBytes() * bitmap.getHeight(); 
    } else { 
     return bitmap.getByteCount(); 
    } 
} 
0

我嘗試了上述所有方法,他們很接近,但並不完全正確(至少對我而言)。

我正在使用bitmap.getByteCount(); sizeof運算()方法的內部創建當一個新的LruCache:

mMemoryCache = new LruCache<String, Bitmap>(cacheSize) { 
    @Override 
    protected int sizeOf(String key, Bitmap bitmap) { 
     return bitmap.getByteCount(); 
    } 
}; 

然後我嘗試了建議:

return bitmap.getRowBytes() * bitmap.getHeight(); 

這是偉大的,但我注意到,返回的值是不同的,當我使用上面的建議,它甚至不會在我的設備上創建緩存。我測試了一個歌Nexus One運行的API 3.2返回值和Galaxy Nexus的運行4.2:

bitmap.getByteCount(); returned-> 15 
bitmap.getRowBytes() * bitmap.getHeight(); returned-> 15400 

所以要解決我的問題,我只是這樣做:

return (bitmap.getRowBytes() * bitmap.getHeight())/1000; 

代替:

return bitmap.getByteCount(); 

可能與您所處的情況不同,但這對我有效。

+0

無論我看,我讀的每個源,我看到它'返回(bitmap.getRowBytes()* bitmap.getHeight() )/ 1000;'。它必須是我正在保存的文件類型或其他。所以下一次我需要使用'bitmap.getByteCount();',我會做一些測試,看看我需要什麼值。 – levibostian

+0

你應該返回'(bitmap.getRowBytes()* bitmap.getHeight())/ 1024;'而不是。 –

0

正如你可以在源代碼中看到,getByteCount很簡單:

public final int getByteCount() { 
    // int result permits bitmaps up to 46,340 x 46,340 
    return getRowBytes() * getHeight(); 
} 

Here is the source code for 5.0

3

這裏的答案是有點過時。原因(在docs):

getByteCount:作爲KITKAT的,這種方法的結果可以不再 可以用於確定的位圖的存儲器使用量。請參閱 getAllocationByteCount()。

那麼,目前的答案應該是:

INT結果= BitmapCompat.getAllocationByteCount(位圖)

或者,如果你堅持自己寫它:

public static int getBitmapByteCount(Bitmap bitmap) { 
    if (VERSION.SDK_INT < VERSION_CODES.HONEYCOMB_MR1) 
     return bitmap.getRowBytes() * bitmap.getHeight(); 
    if (VERSION.SDK_INT < VERSION_CODES.KITKAT) 
     return bitmap.getByteCount(); 
    return bitmap.getAllocationByteCount(); 
}