Just on the code, it's not nonsense.
One: Save to SD card
[Java]View PlainCopy
- File File = new file (Environment.getexternalstoragedirectory (), System.currenttimemillis () +". jpg");
- try {
- out = new FileOutputStream (file);
- Btimage.compress (Bitmap.CompressFormat.JPEG, N, out);
- System.out.println ("___________ saved __sd___ under _______________________");
- } catch (FileNotFoundException e) {
- E.printstacktrace ();
- }
- try {
- Out.flush ();
- Out.close ();
- } catch (IOException e) {
- E.printstacktrace ();
- }
- Toast.maketext (hahitemactivity. This,"save already to" +environment.getexternalstoragedirectory () +"under", Toast.length_short). Show ();
- }
The above method is to directly define the path of a file, and the name of the picture, and then save.
Two: Save bitmap to the specified SD card folder directory
Think also know that this must first have a folder, in order to save the picture in this, then follow the line of thought code it
[Java]View PlainCopy
- if (Environment.getexternalstoragestate () equals (environment.media_mounted)) //Determine if the SDcard can be manipulated
- { //Get SDcard in the specified directory
- String Sdcarddir = environment.getexternalstoragedirectory () + "/coolimage/";
- File Dirfile = new File (Sdcarddir); //directory converted into folders
- if (!dirfile. Exists ()) { //If it does not exist, create this folder
- Dirfile. Mkdirs ();
- } //folder has, you can save pictures
- File File = new file (Sdcarddir, System.currenttimemillis () +". jpg"); Create a picture text in the SDcard directory and name it at the current time
- try {
- out = new FileOutputStream (file);
- Btimage.compress (Bitmap.CompressFormat.JPEG, N, out);
- System.out.println ("_________ saved to ____sd______ under the specified directory folder ____________________");
- } catch (FileNotFoundException e) {
- E.printstacktrace ();
- }
- try {
- Out.flush ();
- Out.close ();
- } catch (IOException e) {
- E.printstacktrace ();
- }
- Toast.maketext (hahitemactivity. This,"save already to" +environment.getexternalstoragedirectory () +"/coolimage/" +"Directory folder", Toast.length_short). Show ();
- }
Note: Access rights Note OH
[Java]View PlainCopy
- <!--Android Read and write SD permissions--
- <uses-permission android:name="Android.permission.READ_EXTERNAL_STORAGE"/>
- <uses-permission android:name="Android.permission.WRITE_EXTERNAL_STORAGE"/>
- T;uses-permission android:name="Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
About the picture of the SD card
Not to be continued
[Java]View PlainCopy
- Save a bitmap picture to an SD card
- protected void Savebitmaptosd (Bitmap bt) {
- File path = Environment.getexternalstoragedirectory ();
- File File = new file (path, System.currenttimemillis () + ". jpg");
- System.out.println (environment.getexternalstoragestate () + "/cool/" +"000000000000000000000000000");
- try {
- FileOutputStream out = new FileOutputStream (file);
- Bt.compress (Bitmap.CompressFormat.JPEG, N, out);
- } catch (FileNotFoundException e) {
- E.printstacktrace ();
- }
- Out.flush ();
- Out.close ();
- }
- /**
- * Save Bitmap to SD card
- * @param bitname saved name
- * @param mbitmap Pictures to Image
- * Return image path After creating a compressed picture
- */
- public static string Savemybitmap (String Bitname,bitmap mbitmap) {
- File F = new file ("/sdcard/" + Bitname + ". png");
- try {
- F.createnewfile ();
- } catch (IOException e) {
- SYSTEM.OUT.PRINTLN ("error in saving Picture:" + e.tostring ());
- }
- FileOutputStream fOut = null;
- try {
- FOut = new FileOutputStream (f);
- } catch (FileNotFoundException e) {
- E.printstacktrace ();
- }
- try {
- Mbitmap.compress (Bitmap.CompressFormat.PNG, fOut);
- } catch (Exception e) {
- return "Create_bitmap_error";
- }
- try {
- Fout.flush ();
- } catch (IOException e) {
- E.printstacktrace ();
- }
- try {
- Fout.close ();
- } catch (IOException e) {
- E.printstacktrace ();
- }
- return "/sdcard/" + Bitname + ". png";
- }
- /**
- * Save Bitmap to SD card
- * @param bitmap
- * @param imagename
- */
- public static string Savebitmaptosdcard (Bitmap Bitmap, string imagename) {
- String path = "/sdcard/" + "img-" + imagename + ". jpg";
- FileOutputStream FOS = null;
- try {
- FOS = New FileOutputStream (path);
- if (fos! = null) {
- Bitmap.compress (Bitmap.CompressFormat.JPEG, N, FOS);
- Fos.close ();
- }
- return path;
- } catch (Exception e) {
- E.printstacktrace ();
- }
- return null;
- }
android-Save the bitmap image to the SD card directory or specify the directory