Android external SD card read

Source: Internet
Author: User

1  Packagecom.kevin.writeorreadfile1_1;2 3 Importandroid.app.Activity;4 ImportAndroid.bluetooth.le.ScanFilter;5 ImportAndroid.graphics.Bitmap;6 Importandroid.graphics.BitmapFactory;7 Importandroid.media.MediaScannerConnection;8 ImportAndroid.os.Bundle;9 Importandroid.os.Environment;Ten ImportAndroid.util.Log; One ImportAndroid.view.View; A ImportAndroid.widget.Button; - ImportAndroid.widget.ImageView; - ImportAndroid.widget.Toast; the  - ImportJava.io.BufferedInputStream; - ImportJava.io.BufferedOutputStream; - ImportJava.io.File; + Importjava.io.FileNotFoundException; - ImportJava.io.FileOutputStream; + Importjava.io.IOException; A ImportJava.io.InputStream; at ImportJava.io.OutputStream; - ImportJava.util.Scanner; -  - /** - * Save and read Data - * 1. Make sure the external SD card is available environment.getexternalstoragestate (); in * 2. Get the root path of the SD card - * 3. Write permissions to * <uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>  + * <uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS" ></uses-permission > - * 4. Read the  */ *  Public classMainactivityextendsActivityImplementsView.onclicklistener { $     Private BooleanState =true;Panax Notoginseng     Privatefile file; -     PrivateImageView img; the     PrivateButton keepbtn,readbtn; + @Override A     protected voidonCreate (Bundle savedinstancestate) { the         Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); -         //Initializing the control $KEEPBTN =(Button) Findviewbyid (r.id.btn_keep_file); $READBTN =(Button) Findviewbyid (r.id.btn_read_file); -IMG =(ImageView) Findviewbyid (r.id.out_img); -  the         //Click events -Keepbtn.setonclicklistener ( This);WuyiReadbtn.setonclicklistener ( This); the  -         //make sure the SD card is available WuString storagestate = Environment.getexternalstoragestate ();//get the status of an SD card -         if(Storagestate.equals (environment.media_mounted)) { AboutLOG.I ("TAG", "Current SD card available"); $}Else { -State =false; -         } -     } A  + @Override the      Public voidOnClick (view view) { -             Switch(View.getid ()) { $                  CaseR.id.btn_keep_file://Click Save the                     if(state) { the saveimg (); the                     } the                      Break; -                  CaseR.id.btn_read_file://Click to read in                     if(state) { the showimg (); the                     } About                      Break; the             } the     } the     // +      Public voidsaveimg () { -Bufferedinputstream is =NULL; theBufferedoutputstream OS =NULL;Bayi         Try { theFile storagedirectory =environment.getexternalstoragedirectory (); theFile =NewFile (Storagedirectory, "yangkai.jpg"); -             //to read a picture in a project -InputStream InputStream =getresources (). Openrawresource (R.drawable.yangkai_photo); theOutputStream OutputStream =Newfileoutputstream (file); the             //Caching mechanism theis =NewBufferedinputstream (inputstream); theOS =NewBufferedoutputstream (outputstream); -             //Read and write operations the             intLength = 0 ; the             byte[] bytes =New byte[1024]; the              while(length = is.read (bytes))! =-1){94Os.write (bytes,0, length); theOs.flush ();//Refresh Operation the             } theToast.maketext ( This, "Picture Saved", Toast.length_short). Show ();98 Scanfile (File.getpath ()); About}Catch(FileNotFoundException e) { - e.printstacktrace ();101}Catch(IOException e) {102 e.printstacktrace ();103}finally {104             if(Is! =NULL){ the                 Try {106 is.close ();107}Catch(IOException e) {108 e.printstacktrace ();109                 } the             }111             if(OS! =NULL){ the                 Try {113 os.close (); the}Catch(IOException e) { the e.printstacktrace (); the                 }117             }118         }119     } - 121     //scan into pictures122     Private voidscanfile (String path) {123 124Mediascannerconnection.scanfile ( This,NewString[]{path},NULL,NULL); the     }126 127     //Show Pictures -      Public voidshowimg () {129         //read it from the file theBitmap Bitmap =Bitmapfactory.decodefile (File.getpath ());131 Img.setimagebitmap (bitmap); the     }133}

Android external SD card read

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.