Fixed. App crash when setting a path without slash
[pub/Android/ownCloud.git] / src / com / owncloud / android / utils / BitmapUtils.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
3 *
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.
7 *
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.
12 *
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/>.
15 *
16 */
17 package com.owncloud.android.utils;
18
19 import android.graphics.Bitmap;
20 import android.graphics.BitmapFactory;
21 import android.graphics.BitmapFactory.Options;
22
23 /**
24 * Utility class with methods for decoding Bitmaps.
25 *
26 * @author David A. Velasco
27 */
28 public class BitmapUtils {
29
30
31 /**
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
34 *
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.
38 * @return
39 */
40 public static Bitmap decodeSampledBitmapFromFile(String srcPath, int reqWidth, int reqHeight) {
41
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;
48 }
49 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
50 options.inMutable = false;
51 }
52
53 // make a false load of the bitmap to get its dimensions
54 options.inJustDecodeBounds = true;
55
56 BitmapFactory.decodeFile(srcPath, options);
57
58 // calculate factor to subsample the bitmap
59 options.inSampleSize = calculateSampleFactor(options, reqWidth, reqHeight);
60
61 // decode bitmap with inSampleSize set
62 options.inJustDecodeBounds = false;
63 return BitmapFactory.decodeFile(srcPath, options);
64
65 }
66
67
68 /**
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.
72 *
73 * @param options Bitmap decoding options; options.outHeight and options.inHeight should
74 * be set.
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.
79 */
80 private static int calculateSampleFactor(Options options, int reqWidth, int reqHeight) {
81
82 final int height = options.outHeight;
83 final int width = options.outWidth;
84 int inSampleSize = 1;
85
86 if (height > reqHeight || width > reqWidth) {
87 final int halfHeight = height / 2;
88 final int halfWidth = width / 2;
89
90 while ((halfHeight / inSampleSize) > reqHeight
91 && (halfWidth / inSampleSize) > reqWidth) {
92 inSampleSize *= 2;
93 }
94 }
95
96 return inSampleSize;
97 }
98
99 }