0

in my App I need to scale an image, this works with normaly. But when a big pictured will be loaded then the decodeStream method returns null. Currently it does not work when the image was bigger than 6 MB (5616x3744)

Does someone know why this happens and what can i do to fix this?

public static Bitmap scaleImage(final String filepath, int height, int width) throws IOException{

    if (filepath == null){
        Log.e(TAG, "cannot create Thumbnail without file");
        return null;
    }

    File f = new File(filepath);
    InputStream is = new FileInputStream(f); 
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(is, null, options);
    is.close();
    final int imageHeight = options.outHeight;
    final int imageWidth = options.outWidth;
    String imageType = options.outMimeType;

    final int thumbnailHeight = height;
    final int thumbnailWidth = width;

    int heightRatio = Math.round((float) imageHeight) / Math.round((float) thumbnailHeight);
    int widthRatio = Math.round((float) imageWidth) / Math.round((float) thumbnailWidth);
    int inSampleSize = 0;
    if (heightRatio < widthRatio){
        inSampleSize =  heightRatio ;
    }
    else{
        inSampleSize =  widthRatio;
    }

    options.inSampleSize = inSampleSize;
    // Decode bitmap with inSampleSize set
    int retrySteps = 0;
    InputStream is2 = new FileInputStream(f);
    Bitmap bmp = null;
    for (int i = 0; i < 5; i++){
        options.inJustDecodeBounds = false;
        try {
            bmp = BitmapFactory.decodeStream(is2, null, options);
            if (bmp != null){
                i = 10;             
            }
            else{
                System.gc();    
                SystemClock.sleep(i * 1000);
                options.inSampleSize = inSampleSize + 1;
            }

        } catch (OutOfMemoryError e) {
            System.gc();    
            SystemClock.sleep(i * 1000);

        }

    }
    is2.close();        
    return bmp;

thanks

Al Phaba
  • 6,545
  • 12
  • 51
  • 83
  • http://developer.android.com/training/displaying-bitmaps/manage-memory.html – Raghunandan Jul 16 '13 at 17:22
  • http://stackoverflow.com/q/7138645/857361 – Ron Jul 16 '13 at 17:23
  • Why are you setting `inJustDecodeBounds` to false? This seems like exactly the right time to use it, in conjunction with `inSampleSize` – Geobits Jul 16 '13 at 18:02
  • @Geobits I did this already in a previous step where I calculated the inSampleSize. In the second step i need to set it to false, so that it will decode – Al Phaba Jul 16 '13 at 18:12
  • Why don't you show us the full code then? Maybe there's something there. Then again, it *is* a bit over 80MB of bitmap data, so it might just be flipping out over that while trying to scale it. – Geobits Jul 16 '13 at 18:14
  • @Geobits can you explain what you mean with "a bit over 80MB" ? I added my complete function maybe you see another reason. Also i added there now some GC action – Al Phaba Jul 16 '13 at 18:33
  • When you say the image is "bigger than 6MB", you're talking about the compressed size of the image. The uncompressed size is `h*w*4` for a typical 4bpp image. That's 84,105,216 bytes in this case. The system still has to decompress the bitmap internally so it can sample it, so it's going to need that memory. I'm guessing you're running into a low memory situation. Does your `catch` block fire? Try adding some logging to it. – Geobits Jul 16 '13 at 18:40
  • @Geobits thanks for the explanation, no the catch block does not fire. It only result in null also after 5 times the GC was called. Maybe its a really too big image for such devices – Al Phaba Jul 16 '13 at 18:50

0 Answers0