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 ();}