Last Updated: September 25, 2018
· alberovalley

Resize an image in android to a given width

High end android devices can take great pictures. But great also means big, and sometimes you need them to have a limited width. Following snippet will get you that result.

// we'll start with the original picture already open to a file
File imgFileOrig = getPic(); //change "getPic()" for whatever you need to open the image file.
Bitmap b = BitmapFactory.decodeFile(imgFileOrig.getAbsolutePath());
// original measurements
int origWidth = b.getWidth();
int origHeight = b.getHeight();

final int destWidth = 600;//or the width you need

if(origWidth > destWidth){
         // picture is wider than we want it, we calculate its target height
          int destHeight = origHeight/( origWidth / destWidth ) ;
         // we create an scaled bitmap so it reduces the image, not just trim it
          Bitmap b2 = Bitmap.createScaledBitmap(b, destWidth, destHeight, false);
          ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            // compress to the format you want, JPEG, PNG... 
            // 70 is the 0-100 quality percentage
    b2.compress(Bitmap.CompressFormat.JPEG,70 , outStream);
            // we save the file, at least until we have made use of it
           File f = new File(Environment.getExternalStorageDirectory()
                    + File.separator + "test.jpg");
           //write the bytes in file
    FileOutputStream fo = new FileOutputStream(f);
           // remember close de FileOutput

5 Responses
Add your response


You can resize the image using amidb purely via Javascript using RESTful APIs.
I have written a blog on this, https://blog.amidb.com/2018/04/tutorial-resize-image-and-save-it-to.html

7 months ago ·


5 months ago ·

You won't ever really feel the should http://androidtvtricks.wikidot.com/ The Guard remote is as distinct as it could get. http://androidtvfunction.greatwebsitebuilder.com/ It lets one discover tens and hundreds https://androidtvgroup.page.tl/ Honestly speaking, it could fingernail your http://androidtvmedia.strikingly.com/ remember the fact that it supports 4k Ultra HD web content. http://www.androidtvmods-rapenura.sitew.in/#You_Must_Know.A shows all set for streaming. :)

3 months ago ·

You need to invest only few minutes https://survey2018.page.tl/ to complete it. It consists of set of questions associated http://getsurveycode.strikingly.com/ with Souplantation and also Dessert Tomatoes restaurants https://www.surveyupdates-radukyra.sitew.in/#Red_Lobster_Survey.A food selections and also their http://surveytimes.greatwebsitebuilder.com/ service throughout your last see.

about 2 months ago ·

You'll definitely enjoy the transportability of the https://routertableonline.puzl.com/ Canine 40-001 if you're a contractor that is https://routertablemarket.sitey.me/ always working in a different place.

about 1 month ago ·