It's not hard, but when you use it, you suddenly can't remember. Record it, please.
Add Permission First
1 < android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
1 Private voidsavecroppedimage (Bitmap bmp) {2File File =NewFile ("/sdcard/myfolder");3 if(!file.exists ())4 File.mkdir ();5 6File =NewFile ("/sdcard/temp.jpg". Trim ());7String FileName =file.getname ();8String mname = filename.substring (0, Filename.lastindexof (".")));9String sName = filename.substring (Filename.lastindexof (".")));Ten One ///sdcard/myfolder/temp_cropped.jpg AString Newfilepath = "/sdcard/myfolder" + "/" + Mname + "_cropped" +SName; -File =NewFile (newfilepath); - Try { the file.createnewfile (); -FileOutputStream fos =Newfileoutputstream (file); -Bmp.compress (Compressformat.jpeg, 50, FOS); - Fos.flush (); + fos.close (); -}Catch(IOException e) { + //TODO auto-generated Catch block A e.printstacktrace (); at } - -}
The more important way here is
Bmp.compress (compressformat.jpeg, FOS);
The documentation is as follows:
Boolean android. Graphics. Bitmap. Compress (compressformat format, int quality, outputstream Stream)
Write a compressed version of the bitmap to the specified outputstream. If This returns true, the bitmap can is reconstructed by passing a corresponding inputstream to Bitmapfactory.decodestream (). Note:not all Formats-bitmap configs directly, so it's possible that the returned bitmap from bitmapfactory C Ould is in a different bitdepth, and/or May has lost per-pixel alpha (e.g. JPEG only supports opaque pixels).
Parameters:
format The format of the compressed image
Quality Hint to the compressor, 0-100.0 meaning compress for small size, and meaning compress for max quality. Some formats, like PNG which is lossless, would ignore the quality setting
Stream The OutputStream to write the compressed data.
Returns:
True if successfully compressed to the specified stream.
The first parameter has three kinds, the source code is as follows
1 /**2 * Specifies the known formats a bitmap can be compressed into3 */4 Public enumCompressformat {5JPEG (0),6PNG (1),7WEBP (2);8 9Compressformat (intnativeint) {Ten This. Nativeint =nativeint; One } A Final intnativeint; -}
Because the original bitmap suffix of the conversion is different, such as the JPG file I used, save it is also a JPG file. But the compressformat.jpeg is used to do the argument.
This parameter should be related to the original file type, this is a knowledge point, later study, write a method based on the type of automatic selection of parameters.
Attach several links:
Android Bitmap Compress (image compression)
Android Chinese API (136)--bitmap
Android save bitmap to specified path