diff options
-rw-r--r-- | src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java | 22 | ||||
-rw-r--r-- | src/de/danoeh/antennapod/util/BitmapDecoder.java | 55 |
2 files changed, 58 insertions, 19 deletions
diff --git a/src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java b/src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java index 3ed331a54..bfd43a08c 100644 --- a/src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java +++ b/src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java @@ -11,6 +11,7 @@ import android.widget.ImageView; import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.R; +import de.danoeh.antennapod.util.BitmapDecoder; public abstract class BitmapDecodeWorkerTask extends AsyncTask<Void, Void, Void> { @@ -88,25 +89,8 @@ public abstract class BitmapDecodeWorkerTask extends f = new File(fileUrl); } if (fileUrl != null && f.exists()) { - BitmapFactory.Options options = new BitmapFactory.Options(); - options.inJustDecodeBounds = true; - BitmapFactory.decodeFile(fileUrl, options); - int sampleSize = calculateSampleSize(options.outWidth, - options.outHeight); - - options.inJustDecodeBounds = false; - options.inSampleSize = sampleSize; - 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) { - bitmap = Bitmap.createScaledBitmap(decodedBitmap, - PREFERRED_LENGTH, PREFERRED_LENGTH, false); + bitmap = BitmapDecoder.decodeBitmap(PREFERRED_LENGTH, fileUrl); + if (bitmap != null) { storeBitmapInCache(bitmap); } else { Log.w(TAG, "Could not load bitmap. Using default image."); 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; + } + } +} |