Android saves the file to the SD card, retrieves the file from the card, and deletes the file.

Source: Internet
Author: User

// Save it to the SD card private static String sdState = Environment. getExternalStorageState (); private static String path = Environment. getExternalStorageDirectory (). toString (); public static void saveBitmap (Bitmap bitmap, String imageName) {File file; File PicName; if (sdState. equals (Environment. MEDIA_MOUNTED) {// get the SD card root directory file = new File (path + "/Huai/TicketsPic"); if (! File. exists () {file. mkdirs ();} PicName = new File (file, imageName); try {if (! PicName. exists () {PicName. createNewFile ();} FileOutputStream fos = new FileOutputStream (PicName); if (PicName. getName (). endsWith (". png ") {bitmap. compress (CompressFormat. PNG, 100, fos);} else if (PicName. getName (). endsWith (". jpg ") {bitmap. compress (CompressFormat. JPEG, 100, fos);} fos. flush (); fos. close ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace () ;}}// get public static Bitmap getBitmap (String imageName) {Bitmap bitmap = null; File imagePic; if (sdState. equals (Environment. MEDIA_MOUNTED) {imagePic = new File (path + "/Huai/TicketsPic", imageName); if (imagePic. exists () {try {bitmap = BitmapFactory. decodeStream (new FileInputStream (imagePic);} catch (FileNotFoundException e) {// e. printStackTrace () ;}}return bitmap;} // Delete the SD card File public static void deleteFile (file File) {if (sdState. equals (Environment. MEDIA_MOUNTED) {if (file. exists () {if (file. isFile () {file. delete ();} // if it is a directory else if (file. isDirectory () {// declare all files in the directory []; File files [] = file. listFiles (); for (int I = 0; I <files. length; I ++) {// traverse all files in the directory deleteFile (files [I]); // iterate each file using this method} file. delete ();}}}

 

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.