android-Save the bitmap image to the SD card directory or specify the directory

Source: Internet
Author: User

Just on the code, it's not nonsense.

One: Save to SD card

[Java]View PlainCopy
  1. File File = new file (Environment.getexternalstoragedirectory (), System.currenttimemillis () +". jpg");
  2. try {
  3. out = new FileOutputStream (file);
  4. Btimage.compress (Bitmap.CompressFormat.JPEG, N, out);
  5. System.out.println ("___________ saved __sd___ under _______________________");
  6. } catch (FileNotFoundException e) {
  7. E.printstacktrace ();
  8. }
  9. try {
  10. Out.flush ();
  11. Out.close ();
  12. } catch (IOException e) {
  13. E.printstacktrace ();
  14. }
  15. Toast.maketext (hahitemactivity.   This,"save already to" +environment.getexternalstoragedirectory () +"under", Toast.length_short). Show ();
  16. }

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
  1. if (Environment.getexternalstoragestate () equals (environment.media_mounted)) //Determine if the SDcard can be manipulated
  2. { //Get SDcard in the specified directory
  3. String Sdcarddir = environment.getexternalstoragedirectory () + "/coolimage/";
  4. File Dirfile = new File (Sdcarddir); //directory converted into folders
  5. if (!dirfile. Exists ()) { //If it does not exist, create this folder
  6. Dirfile. Mkdirs ();
  7. } //folder has, you can save pictures
  8. File File = new file (Sdcarddir, System.currenttimemillis () +". jpg"); Create a picture text in the SDcard directory and name it at the current time
  9. try {
  10. out = new FileOutputStream (file);
  11. Btimage.compress (Bitmap.CompressFormat.JPEG, N, out);
  12. System.out.println ("_________ saved to ____sd______ under the specified directory folder ____________________");
  13. } catch (FileNotFoundException e) {
  14. E.printstacktrace ();
  15. }
  16. try {
  17. Out.flush ();
  18. Out.close ();
  19. } catch (IOException e) {
  20. E.printstacktrace ();
  21. }
  22. Toast.maketext (hahitemactivity. This,"save already to" +environment.getexternalstoragedirectory () +"/coolimage/" +"Directory folder", Toast.length_short).  Show ();
  23. }

Note: Access rights Note OH

[Java]View PlainCopy
    1. <!--Android Read and write SD permissions--
    2. <uses-permission android:name="Android.permission.READ_EXTERNAL_STORAGE"/>
    3. <uses-permission android:name="Android.permission.WRITE_EXTERNAL_STORAGE"/>
    4. T;uses-permission android:name="Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>



About the picture of the SD card
Not to be continued

[Java]View PlainCopy
    1. Save a bitmap picture to an SD card
    2. protected void Savebitmaptosd (Bitmap bt) {
    3. File path = Environment.getexternalstoragedirectory ();
    4. File File = new file (path, System.currenttimemillis () + ". jpg");
    5. System.out.println (environment.getexternalstoragestate () + "/cool/" +"000000000000000000000000000");
    6. try {
    7. FileOutputStream out = new FileOutputStream (file);
    8. Bt.compress (Bitmap.CompressFormat.JPEG, N, out);
    9. } catch (FileNotFoundException e) {
    10. E.printstacktrace ();
    11. }
    12. Out.flush ();
    13. Out.close ();
    14. }
    15. /** 
    16. * Save Bitmap to SD card
    17. * @param bitname saved name
    18. * @param mbitmap Pictures to Image
    19. * Return image path After creating a compressed picture
    20. */
    21. public static string Savemybitmap (String Bitname,bitmap mbitmap) {
    22. File F = new file ("/sdcard/" + Bitname + ". png");
    23. try {
    24. F.createnewfile ();
    25. } catch (IOException e) {
    26. SYSTEM.OUT.PRINTLN ("error in saving Picture:" + e.tostring ());
    27. }
    28. FileOutputStream fOut = null;
    29. try {
    30. FOut = new FileOutputStream (f);
    31. } catch (FileNotFoundException e) {
    32. E.printstacktrace ();
    33. }
    34. try {
    35. Mbitmap.compress (Bitmap.CompressFormat.PNG, fOut);
    36. } catch (Exception e) {
    37. return "Create_bitmap_error";
    38. }
    39. try {
    40. Fout.flush ();
    41. } catch (IOException e) {
    42. E.printstacktrace ();
    43. }
    44. try {
    45. Fout.close ();
    46. } catch (IOException e) {
    47. E.printstacktrace ();
    48. }
    49. return "/sdcard/" + Bitname + ". png";
    50. }
    51. /** 
    52. * Save Bitmap to SD card
    53. * @param bitmap
    54. * @param imagename
    55. */
    56. public static string Savebitmaptosdcard (Bitmap Bitmap, string imagename) {
    57. String path = "/sdcard/" + "img-" + imagename + ". jpg";
    58. FileOutputStream FOS = null;
    59. try {
    60. FOS = New FileOutputStream (path);
    61. if (fos! = null) {
    62. Bitmap.compress (Bitmap.CompressFormat.JPEG, N, FOS);
    63. Fos.close ();
    64. }
    65. return path;
    66. } catch (Exception e) {
    67. E.printstacktrace ();
    68. }
    69. return null;
    70. }

android-Save the bitmap image to the SD card directory or specify the directory

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.