Android cameras take photos, compress them, and store them on the SD card.
Generally, the size of a photo taken by a camera is about 3-4 MB. Therefore, you need to compress the photo because you need to upload it to the server. The demo is stored in the SD card.
I searched for a lot of information online and learned that I can use inSampleSize to set the scaling ratio of the image.
However, note the following:
1) inJustDecodeBounds = true; you must set it to true to obtain only the information of the image. If bitmap is verified at this time, bitmap = null is found;
2) if you need to attach an image, you must reset inJustDecodeBounds = false;
1. achieve image compression (if you see someone else on the internet, you have slightly changed it ):
// CompressBySize (String pathName, int targetWidth, int targetHeight) {BitmapFactory. options opts = new BitmapFactory. options (); opts. inJustDecodeBounds = true; // do not parse the image, but retrieve the header information of the image, including the width and height; Bitmap bitmap = BitmapFactory. decodeFile (pathName, opts); // obtain the image width and height. float imgWidth = opts. outWidth; float imgHeight = opts. outHeight; // calculate the ratio of the image width, height to the target width, and height. Take the smallest integer greater than or equal to the ratio. int widthRatio = (int) Math. ceil (imgWidth/(float) targetWidth); int heightRatio = (int) Math. ceil (imgHeight/(float) targetHeight); opts. inSampleSize = 1; if (widthRatio> 1 | widthRatio> 1) {if (widthRatio> heightRatio) {opts. inSampleSize = widthRatio;} else {opts. inSampleSize = heightRatio;} // After the scaling ratio is set, load the image into the content; opts. inJustDecodeBounds = false; bitmap = BitmapFactory. decodeFile (pathName, opts); return bitmap ;}2. Store the compressed image on the SD card:
// Save it to the SD card public void saveFile (Bitmap bm, String fileName) throws Exception {File dirFile = new File (fileName); // check whether the image exists if (dirFile. exists () {dirFile. delete (); // delete original image} File myCaptureFile = new File (fileName); BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream (myCaptureFile); // 100 indicates no compression, 70 indicates the compression rate is 30% bm. compress (Bitmap. compressFormat. JPEG, 100, bos); bos. flush (); bos. close ();}Note that you need to add a permission to write the SD card:
3. Attach a complete Demo:
1) MainActivity. java
Package com. face. sendwinrar; import java. io. bufferedOutputStream; import java. io. file; import java. io. fileOutputStream; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. OS. bundle; import android. view. menu; import android. view. menuItem; public class MainActivity extends Activity {// photo storage address private static final String FILE_PATH =/sdcard/gone.jpg; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); try {// compress the image Bitmap bitmap = compressBySize (FILE_PATH, 150,200); // Save the image saveFile (bitmap, FILE_PATH);} catch (Exception e) {e. printStackTrace () ;}// the size of the compressed image. public Bitmap compressBySize (String pathName, int targetWidth, int targetHeight) {BitmapFactory. options opts = new BitmapFactory. options (); opts. inJustDecodeBounds = true; // do not parse the image, but retrieve the header information of the image, including the width and height; Bitmap bitmap = BitmapFactory. decodeFile (pathName, opts); // obtain the image width and height. float imgWidth = opts. outWidth; float imgHeight = opts. outHeight; // calculate the ratio of the image width, height to the target width, and height. Take the smallest integer greater than or equal to the ratio. int widthRatio = (int) Math. ceil (imgWidth/(float) targetWidth); int heightRatio = (int) Math. ceil (imgHeight/(float) targetHeight); opts. inSampleSize = 1; if (widthRatio> 1 | widthRatio> 1) {if (widthRatio> heightRatio) {opts. inSampleSize = widthRatio;} else {opts. inSampleSize = heightRatio;} // After the scaling ratio is set, load the image into the content; opts. inJustDecodeBounds = false; bitmap = BitmapFactory. decodeFile (pathName, opts); return bitmap;} // save it to the SD card public void saveFile (Bitmap bm, String fileName) throws Exception {File dirFile = new File (fileName ); // check whether the image contains if (dirFile. exists () {dirFile. delete (); // delete original image} File myCaptureFile = new File (fileName); BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream (myCaptureFile); // 100 indicates no compression, 70 indicates the compression rate is 30% bm. compress (Bitmap. compressFormat. JPEG, 100, bos); bos. flush (); bos. close () ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}@ Override public boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest. xml. int id = item. getItemId (); if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item );}}2) mainfest