Android saves bitmap to the specified path, androidbitmap
It's not hard, but sometimes it's hard to remember .. Record it.
Add permissions first
1 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
1 private void saveCroppedImage(Bitmap bmp) { 2 File file = new File("/sdcard/myFolder"); 3 if (!file.exists()) 4 file.mkdir(); 5 6 file = new File("/sdcard/temp.jpg".trim()); 7 String fileName = file.getName(); 8 String mName = fileName.substring(0, fileName.lastIndexOf(".")); 9 String sName = fileName.substring(fileName.lastIndexOf("."));10 11 // /sdcard/myFolder/temp_cropped.jpg12 String newFilePath = "/sdcard/myFolder" + "/" + mName + "_cropped" + sName;13 file = new File(newFilePath);14 try {15 file.createNewFile();16 FileOutputStream fos = new FileOutputStream(file);17 bmp.compress(CompressFormat.JPEG, 50, fos);18 fos.flush();19 fos.close();20 } catch (IOException e) {21 // TODO Auto-generated catch block22 e.printStackTrace();23 }24 25 }
The most important method here is
bmp.compress(CompressFormat.JPEG, 50, fos);
The document 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 be reconstructed by passing a corresponding inputstream to BitmapFactory. decodeStream (). note: not all Formats support all bitmap configs directly, so it is possible that the returned bitmap from BitmapFactory cocould be in a different bitdepth, and/or may have lost per-pixel alpha (e.g. JPEG only supports opaque pixels ).
Parameters:
FormatThe format of the compressed image
QualityHint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality. Some formats, like PNG which is lossless, will ignore the quality setting
StreamThe outputstream to write the compressed data.
Returns:
True if successfully compressed to the specified stream.
The first parameter has three types. The source code is as follows:
1 /** 2 * Specifies the known formats a bitmap can be compressed into 3 */ 4 public enum CompressFormat { 5 JPEG (0), 6 PNG (1), 7 WEBP (2); 8 9 CompressFormat(int nativeInt) {10 this.nativeInt = nativeInt;11 }12 final int nativeInt;13 }
Because the original bitmap suffixes for conversion are different, for example, the jpg files I used are saved as jpg files. However, CompressFormat. JPEG is used as the parameter.
This parameter should be related to the type of the original file. This is a knowledge point. We will study it in the future and write a method for Automatically Selecting Parameters Based on the type.
Attached links:
Android bitmap compress (image compression)
Android Chinese API (136) -- Bitmap
Android development returns a file through a URL and saves it to the specified SD card path
Public static void writeFileData (String fileName, String message ){
Try {
FileOutputStream output = new FileOutputStream ("/sdcard/" + fileName );
Byte [] bytes = message. getBytes ();
Output. write (bytes );
Output. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
You can specify the file name and write the file.
In android, Bitmap is saved as an image.
You can use the Bitmap. compress function to save a Bitmap object as a PNG or JPG file. Then, you can use BitmapFactory to read the data in the file and generate a Bitmap object.
The saved code is like this:
Try {
FileOutputStream out = new FileOutputStream (filename );
Bmp. compress (Bitmap. CompressFormat. PNG, 90, out );
} Catch (Exception e ){
E. printStackTrace ();
}
For more information, see the help documentation of Bitmap and BitmapFactory.