Android Gets picture size and access to the specified size of thumbnails

How to get the size of the picture? ! 0} # S I5 g + W
idea is very simple:
First, we turn this image into Bitmap, and then use the Bitmap getWidth () and getHeight () method can be used to take the picture width and height.
& V ~ "k% W8} & P Z
new problem again, at the time of the break turn into a Bitmap by BitmapFactory.decodeFile (String path) method, encountered a large number of images, we often encounter OOM (Out Of Memory .) the question how to avoid it do?
it uses BitmapFactory.Options this class we mentioned above.

BitmapFactory.Options this class, there is a field called  inJustDecodeBounds  .SDK in the description of this member is this:
the If the SET to true, the decoder will return null (

no bitmap), but the out ... that is, if we set it to true, then BitmapFactory.decodeFile (String path, Options opt) and not really return to a Bitmap you, it will just its width and height to take back to you, so it will not take up too much memory, it will not be so frequent occurrence of the OOM.

Sample code is as follows:

  1. BitmapFactory.Options options = new BitmapFactory.Options();
  2. options.inJustDecodeBounds = true;
  3. Bitmap bmp = BitmapFactory.decodeFile(path, options);
  4. / * Here return bmp is null *
After this code, options.outWidth and options.outHeight what we want wide and high. Q +, Y! M! W is U. The L $
. 1 D / M B + D3! I7 ~ "C + S Z7
has width and height information, how do we obtain the specified image size is not deformed in a case where a thumbnail image then? 'O% M E7: H7. 3 Y7 ~ M / the Z, L
example, we need to get under the premise of not deformed image as a thumbnail 200 of a width. & n-: X-) Z3 Z "`% U
then we need after scaling calculate the height of the picture is how much . a

int height = options.outHeight * 200 / options.outWidth;

  1. options.outWidth = 200;
  2. options.outHeight = height;' o+ E! m; f2 E  E1 {3 d
  3. / * In order to truly return to you a Bitmap * /
  4. options.inJustDecodeBounds = false;
  5. Bitmap bmp = BitmapFactory.decodeFile(path, options);2 _) }# R8 J+ V* b
  6. image.setImageBitmap(bmp);

Although this we can get our desired size the ImageView 'T9]: the Y G5 G $) * L A
but in the execution BitmapFactory.decodeFile (path, options); when no save memory.
To save memory, also need to use BitmapFactory.Options this class of  inSampleSize  this member variables.
We can calculate this value based on the actual width and height of the picture and we look forward to the width and height.

inSampleSize = options.outWidth / 200;

In addition, in order to save memory, we can use the following several fields:
  1. options.inPreferredConfig = Bitmap.Config.ARGB_4444;    // 默认是Bitmap.Config.ARGB_88887 i1 Z7 x; g6 t, _" P

  2. / * The following two fields need to be used in combination * / . 6 O # Q "{!} & {% A9 F8 U0 D / H
  3. options.inPurgeable = true;
  4. options.inInputShareable = true;
Zhuanzhai to: http://www.lephone.net/thread-1191-1-1.html 

Guess you like

Origin blog.csdn.net/dxh040431104/article/details/6667448