2013-07-24 9 views
14

tôi cần để có được chiều rộng và chiều cao của một bitmap nhưng sử dụng này cung cấp cho một trong số memory exception:Android - chiều rộng và chiều cao của bitmap mà không cần tải nó

Resources res=getResources(); 
    Bitmap mBitmap = BitmapFactory.decodeResource(res, R.drawable.pic); 
    BitmapDrawable bDrawable = new BitmapDrawable(res, mBitmap); 

    //get the size of the image and the screen 
    int bitmapWidth = bDrawable.getIntrinsicWidth(); 
    int bitmapHeight = bDrawable.getIntrinsicHeight(); 

Tôi đọc các giải pháp tại câu hỏi Get bitmap width and height without loading to memory nhưng những gì sẽ là inputStream ở đây?

+0

từ hình ảnh ur là lớn để nạp vào bộ nhớ – KOTIOS

+0

tôi figured mà ra quá .. Do đó câu hỏi làm thế nào để có được các số liệu mà không cần nạp nó trong bộ nhớ – Gooey

Trả lời

15

Bạn cần phải xác định một số BitmapFactory.Options cũng như:

BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inJustDecodeBounds = true; 
BitmapFactory.decodeResource(getResources(), R.id.myimage, options); 
int imageHeight = options.outHeight; 
int imageWidth = options.outWidth; 

bDrawable sẽ không chứa bất kỳ mảng bitmap byte. Lấy từ here: Setting the inJustDecodeBounds property to true while decoding avoids memory allocation, returning null for the bitmap object but setting outWidth, outHeight and outMimeType. This technique allows you to read the dimensions and type of the image data prior to construction (and memory allocation) of the bitmap.

+2

decodeResource sẽ trả về null, nếu options.inJustDecodeBounds = true; Vì vậy, bạn có thể bỏ qua nhiệm vụ 'Bitmap bDrawable = BitmapFactory.de'. Ngoài ra bạn trả lời là sai. bạn phải nhận được widht và heigh từ các tùy chọn – Blackbelt