Android - ширина и высота растрового изображения без загрузки
Мне нужно получить ширину и высоту растрового изображения, но с помощью этого выдается исключение из памяти:
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();
Я прочитал решение на вопрос Получить ширину и высоту растрового изображения без загрузки в память, но какой будет входной поток здесь?
Ответы
Ответ 1
Вам также нужно указать некоторые BitmapFactory.Options
:
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
не будет содержать массив битмап-байтов. Взято из здесь: 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
Используйте этот
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
см. http://developer.android.com/training/displaying-bitmaps/load-bitmap.html