2012-07-27 42 views

回答

28

您可以inJustDecodeBounds設置BitmapFactory.Options以獲取圖像的寬度和高度,而無需加載位圖像素在內存

BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); 
bitmapOptions.inJustDecodeBounds = true; 
BitmapFactory.decodeStream(inputStream, null, bitmapOptions); 
int imageWidth = bitmapOptions.outWidth; 
int imageHeight = bitmapOptions.outHeight; 
inputStream.close(); 

有關詳細信息:

公共布爾inJustDecodeBounds

從以下版本開始:API級別1如果設置爲true,則 解碼器將返回null(無位圖),但out ...字段將仍然爲 設置,允許調用者查詢位圖而不用爲其像素分配內存。

http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inJustDecodeBounds

+1

不能正常工作,因爲該系統可以擴展關於屏幕密度的位圖,當你終於需要加載該位圖。 只有在BitmapFactory.Options.inScaled = false的情況下才會出現這種情況 – Tobliug 2015-08-04 18:13:41