Uploaded image for project: 'Android Authenticator & Contacts'
  1. Android Authenticator & Contacts
  2. ANDAUTH-18

BitmapFactory java.lang.OutOfMemoryError

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • Major
    • 0.3
    • 0.2
    • None

    Description

      To reproduce:
      when downloading some large images, decoding bitmap may cause OutOfMemoryError.
      For 5.98M 4160x3120 JPG , if decoding directly with BitmapFactory, the bitmap need about 4160x3120x4B => 52M memory.

      Solution:
      1. BitmapFactory options.inSampleSize>1 Decode
      // https://developer.android.com/training/displaying-bitmaps/load-bitmap.html#read-bitmap
      // First decode with inJustDecodeBounds=true to check dimensions
      final BitmapFactory.Options options = new BitmapFactory.Options();
      options.inJustDecodeBounds = true;
      BitmapFactory.decodeResource(res, resId, options);

      // Calculate inSampleSize
      options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

      // Decode bitmap with inSampleSize set
      options.inJustDecodeBounds = false;
      return BitmapFactory.decodeResource(res, resId, options);

      2. Another method is downloading the avatar directly on the filesystem instead of byte array as Thomas commented.

      Attachments

        Activity

          People

            fitz Fitz Lee
            fitz Fitz Lee
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: