How to resize Image in Android?

AndroidBitmap

Android Problem Overview


I am creating an application and want to setup a gallery view. I do not want the images in the gallery view to be full size. How do I resize images in Android?

Android Solutions


Solution 1 - Android

Try:

Bitmap yourBitmap;
Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true);

or:

resized = Bitmap.createScaledBitmap(yourBitmap,(int)(yourBitmap.getWidth()*0.8), (int)(yourBitmap.getHeight()*0.8), true);

Solution 2 - Android

public Bitmap resizeBitmap(String photoPath, int targetW, int targetH) {
	BitmapFactory.Options bmOptions = new BitmapFactory.Options();
	bmOptions.inJustDecodeBounds = true;
	BitmapFactory.decodeFile(photoPath, bmOptions);
	int photoW = bmOptions.outWidth;
	int photoH = bmOptions.outHeight;
	
	int scaleFactor = 1;
	if ((targetW > 0) || (targetH > 0)) {
	        scaleFactor = Math.min(photoW/targetW, photoH/targetH);        
	}
	
	bmOptions.inJustDecodeBounds = false;
	bmOptions.inSampleSize = scaleFactor;
	bmOptions.inPurgeable = true; //Deprecated API 21
	
	return BitmapFactory.decodeFile(photoPath, bmOptions);            
}

Solution 3 - Android

Capture the image and resize it.

Bitmap image2 = (Bitmap) data.getExtras().get("data");
img.setImageBitmap(image2);
String incident_ID = IncidentFormActivity.incident_id;

imagepath="/sdcard/RDMS/"+incident_ID+ x + ".PNG";
File file = new File(imagepath);
	try {
		double xFactor = 0;
		double width = Double.valueOf(image2.getWidth());
		Log.v("WIDTH", String.valueOf(width));
		double height = Double.valueOf(image2.getHeight());
		Log.v("height", String.valueOf(height));
		if(width>height){
		xFactor = 841/width;
	}
	else{
		xFactor = 595/width;
	}
				
				
Log.v("Nheight", String.valueOf(width*xFactor));
Log.v("Nweight", String.valueOf(height*xFactor));
int Nheight = (int) ((xFactor*height));
int NWidth =(int) (xFactor * width) ; 
				
bm = Bitmap.createScaledBitmap( image2,NWidth, Nheight, true);
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bm.compress(CompressFormat.PNG, 100, ostream);
ostream.close(); 

Solution 4 - Android

You can use Matrix to resize your camera image ....

BitmapFactory.Options options=new BitmapFactory.Options();
InputStream is = getContentResolver().openInputStream(currImageURI);
bm = BitmapFactory.decodeStream(is,null,options);
int Height = bm.getHeight();
int Width = bm.getWidth();
int newHeight = 300;
int newWidth = 300;
float scaleWidth = ((float) newWidth) / Width;
float scaleHeight = ((float) newHeight) / Height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0,Width, Height, matrix, true);
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

Solution 5 - Android

bm = Bitmap.createScaledBitmap(bitmapSource, width, height, true);

:)

Solution 6 - Android

//photo is bitmap image

Bitmap btm00 = Utils.getResizedBitmap(photo, 200, 200);
  
 setimage.setImageBitmap(btm00);

  And in Utils class :

public static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scaleWidth, scaleHeight);
    // RECREATE THE NEW BITMAP
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
            matrix, false);
    return resizedBitmap;
}

Solution 7 - Android

BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize=2; //try to decrease decoded image 
Bitmap bitmap=BitmapFactory.decodeStream(is, null, options); 
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos); //compressed bitmap to file 

Solution 8 - Android

Following is the function to resize bitmap by keeping the same Aspect Ratio. Here I have also written a detailed blog post on the topic to explain this method. Resize a Bitmap by Keeping the Same Aspect Ratio.

   public static Bitmap resizeBitmap(Bitmap source, int maxLength) {
       try {
           if (source.getHeight() >= source.getWidth()) {
               int targetHeight = maxLength;
               if (source.getHeight() <= targetHeight) { // if image already smaller than the required height
                   return source;
               }

               double aspectRatio = (double) source.getWidth() / (double) source.getHeight();
               int targetWidth = (int) (targetHeight * aspectRatio);

               Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
               if (result != source) {
               }
               return result;
           } else {
               int targetWidth = maxLength;

               if (source.getWidth() <= targetWidth) { // if image already smaller than the required height
                   return source;
               }

               double aspectRatio = ((double) source.getHeight()) / ((double) source.getWidth());
               int targetHeight = (int) (targetWidth * aspectRatio);

               Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
               if (result != source) {
               }
               return result;

           }
       }
       catch (Exception e)
       {
           return source;
       }
   }

Solution 9 - Android

resized = Bitmap.createScaledBitmap(yourImageBitmap,(int)(yourImageBitmap.getWidth()*0.9), (int)(yourBitmap.getHeight()*0.9), true);

Solution 10 - Android

BitmapFactory.Options options=new BitmapFactory.Options();
    		options.inSampleSize = 10;
    		FixBitmap = BitmapFactory.decodeFile(ImagePath, options);
        	//FixBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.gv);
        	
           byteArrayOutputStream = new ByteArrayOutputStream();
           FixBitmap.compress(Bitmap.CompressFormat.JPEG, 80, byteArrayOutputStream); //compress to 50% of original image quality
           byteArray = byteArrayOutputStream.toByteArray();
           
           ConvertImage = Base64.encodeToString(byteArray, Base64.DEFAULT);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJignesh AnsodariyaView Question on Stackoverflow
Solution 1 - AndroidgoodmView Answer on Stackoverflow
Solution 2 - AndroidHernaldo GonzalezView Answer on Stackoverflow
Solution 3 - AndroidJay MayuView Answer on Stackoverflow
Solution 4 - AndroidN.DroidView Answer on Stackoverflow
Solution 5 - AndroidMiscaView Answer on Stackoverflow
Solution 6 - Androidritesh4326View Answer on Stackoverflow
Solution 7 - AndroidvoidRyView Answer on Stackoverflow
Solution 8 - AndroidAsad Ali ChoudhryView Answer on Stackoverflow
Solution 9 - AndroidVaishali SutariyaView Answer on Stackoverflow
Solution 10 - AndroidAlobaidiView Answer on Stackoverflow