Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Bitmap size exceeds 32 bit in DialogFragment

This is not a duplicate of Mysterious stacktrace in Android developer console (bitmap size exceeds 32bits)

That question didn't provide a single line of code and there's no answer either; besides, I get this error even if I set the Bitmap size to 32x32, it is the only Bitmap, so it is not memory related.

What I want to do

  1. Get the size of an ImageView,
  2. create a BitMap
  3. and draw something on a Canvas.

The Log.e output for the size of the ImageView and therefore Bitmap is:

Width: 272
Height: 136

What happens

The below code works well on Nexus4, Nexus7 and Desire HD (CM10), but running the app on the emulator gives me the error shown below (API 8).

Findings

  1. I've tried it with half the size and 32x32, which gives the same error.

  2. I am displaying the ImageView in a DialogFragment (ActionBarSherlock/HoloEveryWhere for API 8), maybe that's the culprit?

ImageView:

<ImageView
      android:id="@+id/imageView1"
      android:layout_width="match_parent"
      android:layout_height="1dp"
      android:layout_marginBottom="12dp"
      android:layout_marginTop="12dp" />

In my activity:

iv = (ImageView) view.findViewById(R.id.imageView1);
ViewTreeObserver vto = iv.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
    @Override
    public boolean onPreDraw() {
         if (!waveFormMeasured) {
             if (iv.getMeasuredWidth() > 1) {
                 width = iv.getMeasuredWidth();
                 Log.e(TAG, "Width: " + width + " Height: " + width / 2);
                 waveBitmap = Bitmap.createBitmap((int) width, (int) ((int) width / 2), Config.RGB_565);
                 Log.e(TAG, "Bitmap created");
                 waveCanvas = new Canvas(waveBitmap);
                 Log.e(TAG, "Bitmap set to Canvas");
                 iv.getLayoutParams().height = width / 2;
                 Log.e(TAG, "ImageView Height changed");
                 iv.setImageBitmap(waveBitmap);
                 Log.e(TAG, "Bitmap set to ImageView");
                 drawWaveForm(true);
                 Log.e(TAG, "WaveForm drawn");
                 waveFormMeasured = true;
              }
          }
     return true;
     }
  });

What is wrong here ?

02-21 17:12:48.301: E/Drummers(375): Width: 272 Height: 136
02-21 17:12:48.301: E/Drummers(375): Bitmap created
02-21 17:12:48.301: E/Drummers(375): Bitmap set to Canvas
02-21 17:12:48.301: E/Drummers(375): ImageView Height changed
02-21 17:12:48.301: E/Drummers(375): Bitmap set to ImageView
02-21 17:12:48.623: E/Drummers(375): WaveForm drawn
02-21 17:12:48.842: E/AndroidRuntime(375): FATAL EXCEPTION: main
02-21 17:12:48.842: E/AndroidRuntime(375): java.lang.IllegalArgumentException: bitmap size exceeds 32bits
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.graphics.Bitmap.nativeCreate(Native Method)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.View.buildDrawingCache(View.java:6577)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.onAnimationStart(ViewGroup.java:1259)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.drawChild(ViewGroup.java:1505)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.View.draw(View.java:6883)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.View.draw(View.java:6986)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.widget.ScrollView.draw(ScrollView.java:1409)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.View.buildDrawingCache(View.java:6640)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.onAnimationStart(ViewGroup.java:1259)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.drawChild(ViewGroup.java:1505)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.View.draw(View.java:6883)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
02-21 17:12:48.842: E/AndroidRuntime(375):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewRoot.draw(ViewRoot.java:1522)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.os.Looper.loop(Looper.java:123)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-21 17:12:48.842: E/AndroidRuntime(375):  at java.lang.reflect.Method.invokeNative(Native Method)
02-21 17:12:48.842: E/AndroidRuntime(375):  at java.lang.reflect.Method.invoke(Method.java:507)
02-21 17:12:48.842: E/AndroidRuntime(375):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-21 17:12:48.842: E/AndroidRuntime(375):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-21 17:12:48.842: E/AndroidRuntime(375):  at dalvik.system.NativeStart.main(Native Method)
like image 789
stefple Avatar asked Nov 03 '22 04:11

stefple


1 Answers

bitmaps have a size limit that depends on the device. The size is generally the size of the screen itself. There are several questions on stack overflow that address this issue, such as OutOfMemoryError: bitmap size exceeds VM budget :- Android or Strange out of memory issue while loading an image to a Bitmap object . If you need a very large image, you can simply break it down into more manageable sizes, and place them in multiple ImageViews. I created an app several months ago with a large, scrollable image that was about 15 images strung together seamlessly - validation that this is a good way to go.

On a final note, bitmaps are difficult to work with directly, due to the memory issues they cause on Android. Look into BitmapFactory.Options, and always remember to nullify and recycle unused bitmaps.

Edit

It may also have something to do with modifying the contents of the View in onPreDraw(). This post: When Can I First Measure a View? discusses how editing the size of a View in this method will cause onPreDraw() to continually be called, whereas performing these operations with an onGlobalLayoutListener will give the expected results.

like image 93
Phil Avatar answered Nov 09 '22 18:11

Phil