Wednesday, October 19, 2011

Resize the image

By using below code we can resize the image


            Bitmap bitmap  = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream());
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            int newWidth = 100;
            int newHeight = 100;

            // calculate the scale - in this case = 0.4f
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;

            // createa matrix for the manipulation
            Matrix matrix = new Matrix();
            // resize the bit map
            matrix.postScale(scaleWidth, scaleHeight);

            Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                    width, height, matrix, true);

            ((ImageView) findViewById(R.id.ImageviewTest)).setImageBitmap(resizedBitmap);