summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/util/BitmapDecoder.java
diff options
context:
space:
mode:
authordaniel oeh <daniel.oeh@gmail.com>2012-08-03 13:37:28 +0200
committerdaniel oeh <daniel.oeh@gmail.com>2012-08-03 13:37:28 +0200
commitfadb0de18c6beb263324e8771bb54a496f55e9a6 (patch)
tree9392cfba8ffbfa4f7426efcdf8f53643252e216f /src/de/danoeh/antennapod/util/BitmapDecoder.java
parentfde6a05f1816c278e307bc84356c561b420559e9 (diff)
downloadAntennaPod-fadb0de18c6beb263324e8771bb54a496f55e9a6.zip
Moved Bitmap decoding methods into separate class
Diffstat (limited to 'src/de/danoeh/antennapod/util/BitmapDecoder.java')
-rw-r--r--src/de/danoeh/antennapod/util/BitmapDecoder.java55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/de/danoeh/antennapod/util/BitmapDecoder.java b/src/de/danoeh/antennapod/util/BitmapDecoder.java
new file mode 100644
index 000000000..8cd0bd910
--- /dev/null
+++ b/src/de/danoeh/antennapod/util/BitmapDecoder.java
@@ -0,0 +1,55 @@
+package de.danoeh.antennapod.util;
+
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.util.Log;
+
+public class BitmapDecoder {
+ private static final String TAG = "BitmapDecoder";
+
+ private static int calculateSampleSize(int preferredLength, int width, int height) {
+ int max = Math.max(width, height);
+ if (max < preferredLength) {
+ return 1;
+ } else {
+ // find first sample size where max / sampleSize <
+ // PREFERRED_LENGTH
+ for (int sampleSize = 1, power = 0;; power++, sampleSize = (int) Math
+ .pow(2, power)) {
+ int newLength = max / sampleSize;
+ if (newLength <= preferredLength) {
+ if (newLength > 0) {
+ return sampleSize;
+ } else {
+ return sampleSize - 1;
+ }
+ }
+ }
+ }
+ }
+
+ public static Bitmap decodeBitmap(int preferredLength, String fileUrl) {
+ BitmapFactory.Options options = new BitmapFactory.Options();
+ options.inJustDecodeBounds = true;
+ BitmapFactory.decodeFile(fileUrl, options);
+ int sampleSize = calculateSampleSize(preferredLength, options.outWidth,
+ options.outHeight);
+
+ options.inJustDecodeBounds = false;
+ options.inSampleSize = sampleSize;
+ Bitmap decodedBitmap = BitmapFactory.decodeFile(fileUrl,
+ options);
+ if (decodedBitmap == null) {
+ Log.i(TAG,
+ "Bitmap could not be decoded in custom sample size. Trying default sample size (path was "
+ + fileUrl + ")");
+ decodedBitmap = BitmapFactory.decodeFile(fileUrl);
+ }
+ if (decodedBitmap != null) {
+ return Bitmap.createScaledBitmap(decodedBitmap,
+ preferredLength, preferredLength, false);
+ } else {
+ return null;
+ }
+ }
+}