1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package com
.owncloud
.android
.utils
;
19 import android
.graphics
.Bitmap
;
20 import android
.graphics
.BitmapFactory
;
21 import android
.graphics
.BitmapFactory
.Options
;
24 * Utility class with methods for decoding Bitmaps.
26 * @author David A. Velasco
28 public class BitmapUtils
{
32 * Decodes a bitmap from a file containing it minimizing the memory use, known that the bitmap
33 * will be drawn in a surface of reqWidth x reqHeight
35 * @param srcPath Absolute path to the file containing the image.
36 * @param reqWidth Width of the surface where the Bitmap will be drawn on, in pixels.
37 * @param reqHeight Height of the surface where the Bitmap will be drawn on, in pixels.
40 public static Bitmap
decodeSampledBitmapFromFile(String srcPath
, int reqWidth
, int reqHeight
) {
42 // set desired options that will affect the size of the bitmap
43 final Options options
= new Options();
44 options
.inScaled
= true
;
45 options
.inPurgeable
= true
;
46 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.GINGERBREAD_MR1
) {
47 options
.inPreferQualityOverSpeed
= false
;
49 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.HONEYCOMB
) {
50 options
.inMutable
= false
;
53 // make a false load of the bitmap to get its dimensions
54 options
.inJustDecodeBounds
= true
;
56 BitmapFactory
.decodeFile(srcPath
, options
);
58 // calculate factor to subsample the bitmap
59 options
.inSampleSize
= calculateSampleFactor(options
, reqWidth
, reqHeight
);
61 // decode bitmap with inSampleSize set
62 options
.inJustDecodeBounds
= false
;
63 return BitmapFactory
.decodeFile(srcPath
, options
);
69 * Calculates a proper value for options.inSampleSize in order to decode a Bitmap minimizing
70 * the memory overload and covering a target surface of reqWidth x reqHeight if the original
71 * image is big enough.
73 * @param options Bitmap decoding options; options.outHeight and options.inHeight should
75 * @param reqWidth Width of the surface where the Bitmap will be drawn on, in pixels.
76 * @param reqHeight Height of the surface where the Bitmap will be drawn on, in pixels.
77 * @return The largest inSampleSize value that is a power of 2 and keeps both
78 * height and width larger than reqWidth and reqHeight.
80 private static int calculateSampleFactor(Options options
, int reqWidth
, int reqHeight
) {
82 final int height
= options
.outHeight
;
83 final int width
= options
.outWidth
;
86 if (height
> reqHeight
|| width
> reqWidth
) {
87 final int halfHeight
= height
/ 2;
88 final int halfWidth
= width
/ 2;
90 while ((halfHeight
/ inSampleSize
) > reqHeight
91 && (halfWidth
/ inSampleSize
) > reqWidth
) {