Android - Update Bitmap When the View Has Been Measured
<p>I am trying to retrieve a bitmap from an image view and then scale it. But I am getting the error below:</p>

<p>"java.lang.IllegalArgumentException: width and height must be > 0"</p>

<p>As ı searched the internet it is because before the imageView finishes to be measured and converted to bitmap, I try to update the bitmap. Could anybody help me abouy how I can fix this. My code is as below:</p>

<pre><code>profileImage.buildDrawingCache();
Bitmap bm = profileImage.getDrawingCache();

int widthPixel = bm.getWidth();
int heightPixel = bm.getHeight();
int basePixel;
float widthRatio;
float heightRatio;

if (widthPixel &lt; heightPixel) {
  basePixel = widthPixel;
}
else {
   basePixel = heightPixel;
}

if (basePixel &gt; 180) {
  widthRatio = 180/basePixel;
  heightRatio = 180/basePixel;
}
else {
   widthRatio = 1;
   heightRatio = 1;
}

Bitmap bmResized = Bitmap.createScaledBitmap(bm,(int)(bm.getWidth()*widthRatio), (int)(bm.getHeight()*heightRatio), true);
// bm.recycle();

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmResized.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byteArray1 = stream.toByteArray();
</code></pre>

<p>Edited Code (now getting "java.lang.IllegalArgumentException: value may not be null." error):</p>

<pre><code>                                    profileImage.buildDrawingCache();
                                    bm = profileImage.getDrawingCache();



                                    profileImage.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
                                        public boolean onPreDraw() {
                                            profileImage.getViewTreeObserver().removeOnPreDrawListener(this);

                                            widthPixel = profileImage.getMeasuredWidth();
                                            heightPixel = profileImage.getMeasuredHeight();

                                            if (widthPixel &lt; heightPixel) {

                                                basePixel = widthPixel;

                                            }

                                            else {

                                                basePixel = heightPixel;

                                            }


                                            if (basePixel &gt; 180) {

                                                widthRatio = 180/basePixel;
                                                heightRatio = 180/basePixel;


                                            }

                                            else {

                                                widthRatio = 1;
                                                heightRatio = 1;

                                            }


                                            Bitmap bmResized = Bitmap.createScaledBitmap(bm,(int)(bm.getWidth()*widthRatio), (int)(bm.getHeight()*heightRatio), true);
                                            // bm.recycle();

                                            ByteArrayOutputStream stream = new ByteArrayOutputStream();
                                            bmResized.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                                            byteArray1 = stream.toByteArray();

                                            image1 = new ParseFile("profilePhoto.jpg", byteArray1, "image/jpg");

                                            return true;
                                        }
                                    });
</code></pre>