diff options
author | Bananeweizen <bananeweizen@gmx.de> | 2011-11-25 09:08:01 +0100 |
---|---|---|
committer | Bananeweizen <bananeweizen@gmx.de> | 2011-11-25 09:08:01 +0100 |
commit | ea1130c23e83b28b7421d9522c64fb4233fb1a3c (patch) | |
tree | 368acc076a09d13c5547141897c10fa0917df3c6 /main/src/cgeo/geocaching/network | |
parent | 2fe7298b27f494201e81d340449221adc719f9a0 (diff) | |
download | cgeo-ea1130c23e83b28b7421d9522c64fb4233fb1a3c.zip cgeo-ea1130c23e83b28b7421d9522c64fb4233fb1a3c.tar.gz cgeo-ea1130c23e83b28b7421d9522c64fb4233fb1a3c.tar.bz2 |
fix: avoid out of memory when decoding large bitmaps
Diffstat (limited to 'main/src/cgeo/geocaching/network')
-rw-r--r-- | main/src/cgeo/geocaching/network/HtmlImage.java | 38 |
1 files changed, 25 insertions, 13 deletions
diff --git a/main/src/cgeo/geocaching/network/HtmlImage.java b/main/src/cgeo/geocaching/network/HtmlImage.java index 83873cc..0258111 100644 --- a/main/src/cgeo/geocaching/network/HtmlImage.java +++ b/main/src/cgeo/geocaching/network/HtmlImage.java @@ -22,6 +22,9 @@ import android.view.Display; import android.view.WindowManager; import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; import java.util.Date; public class HtmlImage implements Html.ImageGetter { @@ -181,26 +184,35 @@ public class HtmlImage implements Html.ImageGetter { private Bitmap loadCachedImage(final File file) { if (file.exists()) { if (reason > 0 || file.lastModified() > (new Date().getTime() - (24 * 60 * 60 * 1000))) { - setSampleSize(file.length()); + setSampleSize(file); return BitmapFactory.decodeFile(file.getPath(), bfOptions); } } return null; } - private void setSampleSize(final long imageSize) { - // large images will be downscaled on input to save memory - if (imageSize > (6 * 1024 * 1024)) { - bfOptions.inSampleSize = 48; - } else if (imageSize > (4 * 1024 * 1024)) { - bfOptions.inSampleSize = 16; - } else if (imageSize > (2 * 1024 * 1024)) { - bfOptions.inSampleSize = 10; - } else if (imageSize > (1 * 1024 * 1024)) { - bfOptions.inSampleSize = 6; - } else if (imageSize > (0.5 * 1024 * 1024)) { - bfOptions.inSampleSize = 2; + private void setSampleSize(final File file) { + //Decode image size only + BitmapFactory.Options options = new BitmapFactory.Options(); + options.inJustDecodeBounds = true; + + try { + FileInputStream fis = new FileInputStream(file); + BitmapFactory.decodeStream(fis, null, options); + fis.close(); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + int scale = 1; + if (options.outHeight > maxHeight || options.outWidth > maxWidth) { + scale = Math.max(options.outHeight / maxHeight, options.outWidth / maxWidth); } + bfOptions.inSampleSize = scale; } private static boolean isCounter(final String url) { |