I would like to reduce My Bitmap image size to maximum of 640px. For example I have Bitmap image of size 1200 x 1200 px .. How can I reduce it to 640px.
Asked
Active
Viewed 4.4k times
4 Answers
102
If you pass bitmap width
and height
then use:
public Bitmap getResizedBitmap(Bitmap image, int bitmapWidth, int bitmapHeight) {
return Bitmap.createScaledBitmap(image, bitmapWidth, bitmapHeight, true);
}
If you want to keep the bitmap ratio the same, but reduce it to a maximum side length, use:
public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
int width = image.getWidth();
int height = image.getHeight();
float bitmapRatio = (float) width / (float) height;
if (bitmapRatio > 1) {
width = maxSize;
height = (int) (width / bitmapRatio);
} else {
height = maxSize;
width = (int) (height * bitmapRatio);
}
return Bitmap.createScaledBitmap(image, width, height, true);
}

davidweitzenfeld
- 1,021
- 2
- 15
- 38

Divyang Metaliya
- 1,908
- 1
- 12
- 14
-
8Thanks for this snippet. There is an error tough, you must check "if (bitmapRatio > 1)" not 0. Even if height is bigger, you will not have a negative ratio. – ClemM Mar 07 '14 at 13:42
-
I see this solution everywhere. But why it crops my bitmap? I only have left top part and it is shifted to the right of screen as well( – Sermilion Aug 19 '16 at 19:19
15
Use this Method
/** getResizedBitmap method is used to Resized the Image according to custom width and height
* @param image
* @param newHeight (new desired height)
* @param newWidth (new desired Width)
* @return image (new resized image)
* */
public static Bitmap getResizedBitmap(Bitmap image, int newHeight, int newWidth) {
int width = image.getWidth();
int height = image.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0, width, height,
matrix, false);
return resizedBitmap;
}

Usman Kurd
- 7,212
- 7
- 57
- 86
13
or you can do it like this:
Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter);
Passing filter = false will result in a blocky, pixellated image.
Passing filter = true will give you smoother edges.

SoftWyer
- 1,986
- 28
- 33

Marko Niciforovic
- 3,561
- 2
- 21
- 28
-
-
5Did you downvote? What do you mean it doesn't exists? Bitmap.createScaledBitmap exists since api lvl 1 – Marko Niciforovic Jul 14 '14 at 11:57
0
Here is working code to reduce Bitmap image resolution (pixels) to desired value...
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.Toast;
public class ImageProcessActivity extends AppCompatActivity {
private static final String TAG = "ImageProcessActivity";
private static final String IMAGE_PATH = "/sdcard/DCIM/my_image.jpg";
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
public static Bitmap decodeSampleDrawableFromFile(String file, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(file, options);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_process);
new AsyncTask<Object, Object, Bitmap>() {
@Override
protected Bitmap doInBackground(Object... objects) {
try {
return decodeSampleDrawableFromFile(IMAGE_PATH, 640, 640);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
((ImageView) findViewById(R.id.img)).setImageBitmap(bitmap);
}
}.execute();
}
}
Steps:
Get the
Bitmap.Options
(image info).Sample the size to desired sample size.
Load to
Bitmap
with given options (desired resolution) from the image file into abitmap
object. But do this operation in a background thread.Load
Bitmap
image intoImageView
on UI thread (onPostExecute()
).

Rahul Raina
- 3,322
- 25
- 30