Android saves bitmap objects in PNG format in internal storage

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.