In the case of image processing in Android, sometimes we want to save the processed results in the format of the image file in the internal storage space, this article, for this purpose, describes the method of saving the data of the bitmap object in PNG format.
1. Add Permissions
Because it is the SD card operation, it is necessary to add read and write permissions for your program, you need to add the following:
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>
<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS" ></uses-permission>
These two permissions are briefly explained as follows:
"Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"-allows removable storage of mounted and anti-mount file systems
"Android.permission.WRITE_EXTERNAL_STORAGE"-Permissions to create folders in SDcard in the emulator
2, save the image of the relevant code
The code is relatively simple, where the storage location is the absolute path to write, and you can get different location paths by using environment.
Tips: When using this function, remember to bring the extension of the file.
Private voidSavebitmap (Bitmap bitmap,string bitname)throwsIOException {File file=NewFile ("/sdcard/dcim/camera/" +bitname); if(File.exists ()) {file.delete (); } FileOutputStream out; Try{ out=Newfileoutputstream (file); if(Bitmap.compress (Bitmap.CompressFormat.PNG, 90, out)) {Out.flush (); Out.close (); } } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } }
Android saves bitmap objects in PNG format in internal storage