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