wzinww
Last Updated: September 13, 2018
·
38.54K
· 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");
           f.createNewFile();
           //write the bytes in file
    FileOutputStream fo = new FileOutputStream(f);
    fo.write(outStream.toByteArray());
           // remember close de FileOutput
    fo.close();
}

4 Responses
Add your response

29806

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

5 months ago ·
29999

thanks

3 months ago ·
30360

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. :)

about 1 month ago ·
30686

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.

7 days ago ·