"Android" save bitmap to SD card

Source: Internet
Author: User

1. Open the Read and write SD card permissions

You need to add the following code to Androidmanifest.xml:

<uses-permission android:name= "Android.permission.INTERNET"/> <uses-permission android:name= " Android.permission.WRITE_EXTERNAL_STORAGE "/>


The first method:

 Public  voidSavebitmap (String bitname, Bitmap mbitmap) {File F=NewFile ("/sdcard/" + Bitname + ". png");Try{f.createnewfile ();}Catch(IOException e) {tools.toastshort ("An error occurred while saving the picture:" +e.tostring ());} FileOutputStream FOut=NULL;Try{FOut=NewFileOutputStream (f);} Catch(FileNotFoundException e) {e.printstacktrace ();} Mbitmap.compress (Bitmap.CompressFormat.PNG,100, fOut);Try{Fout.flush ();}Catch(IOException e) {e.printstacktrace ();}Try{fout.close ();}Catch(IOException e) {e.printstacktrace ();}}


The second method:

1.

 Public BooleanWritepngfile (File outFile) {//Save graphics drawn on the screen to an SD cardBooleanResault =false;//store identity, false for Save failedTry{FileOutputStream fos=NewFileOutputStream (OutFile);//Create a file output stream (write file)if(Editbitmap[0].compress (Bitmap.CompressFormat.PNG, FOS)) {//compresses the Picture object in PNG format (mass 100%), writes the fileResault =true;//Storage Success}fos.flush (); //RefreshFos.close ();//Close the stream}Catch(Exception e) {e.printstacktrace ();}returnResault;}

2.

 Public voidSavebitmap () {Final intFileIndex = getsharedpreferences ("Bitmapindex", Context.mode_private). GetInt ("Index", 0);//Remove the file serial number from the shared preferences record for the first time starting from 0NewAlertdialog.builder ( This). Settitle ("hint message")//Create and display a prompt dialog box. SetIcon (Android. R.drawable.ic_menu_manage)//Set icon. Setmessage ("Save to SD card: June porcelain" + FileIndex + ". png?") ")//Set Prompt Information. Setpositivebutton ("OK",NewOnclicklistener () {//pressing the "OK" button for processing Public voidOnClick (Dialoginterface dialog,intwhich) {File OutFile=NewFile (Environment.getexternalstoragedirectory (). GetAbsolutePath ()+File.separator+ "June porcelain/June porcelain" +FileIndex+ ". png");//Create a new file on the SD cardif(MyCanvas.editArea.writePngFile (OutFile)) {//draws the path to the bitmap and compresses the savedGetsharedpreferences ("Bitmapindex", Context.mode_private). Edit (). Putint ("Index", (FileIndex+ 1)% 5). commit ();//serial number loop increment 0~4Tools.toastshort ("Saved successfully! "); Issave=false;if(Index = = 2) {Screennum= 1; MyCanvas.menu.creat ();}}}). Setnegativebutton ("Cancel",NewOnclicklistener () {//Press the Cancel button to process Public voidOnClick (Dialoginterface dialog,intwhich) {Issave=false;if(Index = = 2) {Screennum= 1; MyCanvas.menu.creat ();}}). Create (). Show ();}

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.