Save Bitmap to SD card, save BitmapSD card

Source: Internet
Author: User

Save Bitmap to SD card, save BitmapSD card

Public static void saveBitmapInExternalStorage (Bitmap bitmap, Context context) {try {if (IsExternalStorageAvailableAndWriteable () {File extStorage = new File (Environment. getExternalStorageDirectory (). getPath () + "/orimuse"); // orimuse is the next folder on the SD card if (! ExtStorage. exists () {extStorage. mkdirs ();} File file = new File (extStorage, System. currentTimeMillis () + ". png "); FileOutputStream fOut = new FileOutputStream (file); bitmap. compress (Bitmap. compressFormat. PNG, 90, fOut); // compress the image fOut. flush (); fOut. close (); Toast. makeText (context, "saved successfully", Toast. LENGTH_SHORT ). show ();} else {Toast. makeText (context, "failed to save", Toast. LENGTH_SHORT ). show () ;}} catch (IOException ioe) {ioe. printStackTrace ();}}


How does android save an array as an image and save it on the SD card?

Bitmap bm = BitmapFactory. decodeByteArray (byte [] data, int offset, int length); Do not forget to judge whether the array is empty.

Save ....
Public void saveFile (Bitmap bm, String fileName) throws IOException {
Private final static String ALBUM_PATH
= Environment. getExternalStorageDirectory () + "/download_test /";

File dirFile = new File (ALBUM_PATH );
If (! DirFile. exists ()){
DirFile. mkdir ();
}
File myCaptureFile = new File (ALBUM_PATH + fileName );
BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream (myCaptureFile ));
Bm. compress (Bitmap. CompressFormat. JPEG, 80, bos );
Bos. flush ();
Bos. close ();
}

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.

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.