First remember to add Read permissions
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.dell.wodelianxi" > <uses-permission android:name= "android.permission.MOUNT_UNMOUNT_ Filesystems "/> <uses-permission android:name=" Android.permission.WRITE_EXTERNAL_STORAGE "/> <Application Android:allowbackup= "true"Android:icon= "@mipmap/ic_launcher"Android:label= "@string/app_name"Android:supportsrtl= "true"Android:theme= "@style/apptheme" > <activity android:name= ". Sdkacunchu" > <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.catego Ry. LAUNCHER "/> </intent-filter> </activity> <activity android:name=". Gridview "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/> <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name= ". Baseadaptershixian" > <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.cat Egory. LAUNCHER "/> </intent-filter> </activity> <activity android:name=". Tupianlunbo "> <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:n Ame= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activit Y android:name= ". Denglucunchu" > <intent-filter> <action android:name= "android.intent . Action. MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filte R> </activity> </application></manifest>
XML file
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context= "Com.example.dell.wodelianxi.sdkacunchu"android:orientation= "Vertical" > <EditText android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "Input Content"Android:id= "@+id/et_shu"/> <EditText android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:id= "@+id/et_du"Android:hint= "Read Content"/> <LinearLayout android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" > <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Save to Directory"Android:onclick= "Baocun"/> <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Read Directory"Android:onclick= "Duqu"/> </LinearLayout><LinearLayout android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" > <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Save to Custom Directory"Android:onclick= "Baocunzidingyi"/> <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "read from definition directory"Android:onclick= "Duquzidingyi"/></linearlayout></linearlayout>
Java code
PackageCom.example.dell.wodelianxi;ImportAndroid.os.Bundle;Importandroid.os.Environment;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.View;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream; Public classSdkacunchuextendsappcompatactivity {EditText Et_shu; EditText Et_du; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_sdkacunchu); } Public voidBaocun (view view) {if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {String path= Getexternalfilesdir (NULL). GetAbsolutePath (); Path+ = "/tt"; Et_shu=(EditText) Findviewbyid (R.id.et_shu); String content=Et_shu.gettext (). toString (); Try{FileOutputStream fos=NewFileOutputStream (path); Fos.write (Content.getbytes ("Utf-8")); Fos.close (); } Catch(Exception e) {e.printstacktrace (); } } Else{toast.maketext (Sdkacunchu). This, "No SD card found", Toast.length_short). Show (); } } Public voidDuqu (View v) {if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {String path= Getexternalfilesdir (NULL). GetAbsolutePath (); Path+ = "/tt"; Try{FileInputStream fis=NewFileInputStream (path); byte[] B =New byte[1024]; intI =0; StringBuilder Str=NewStringBuilder (); while((I=fis.read (b)) >0) {str.append (NewString (b,0, i)); } Et_du=(EditText) Findviewbyid (R.ID.ET_DU); Et_du.settext (str); Fis.close (); } Catch(Exception e) {e.printstacktrace (); } } Else{toast.maketext (Sdkacunchu). This, "No SD card found", Toast.length_short). Show (); } } Public voidBaocunzidingyi (View v) {if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {String path=environment.getexternalstoragedirectory (). GetAbsolutePath (); Path+ = "/1126"; Et_shu=(EditText) Findviewbyid (R.id.et_shu); String Context=Et_shu.gettext (). toString (); File File=NewFile (path); if(!file.exists ()) {File.mkdir (); } Path+ = "/tt"; Try{FileOutputStream fos=NewFileOutputStream (path); Fos.write (Context.getbytes ("Utf-8")); Fos.close (); } Catch(Exception e) {e.printstacktrace (); } } Else{toast.maketext (Sdkacunchu). This, "No SD card found", Toast.length_short). Show (); } } Public voidDuquzidingyi (View v) {if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {String path=environment.getexternalstoragedirectory (). GetAbsolutePath (); Path+ = "/1126/tt"; Et_du=(EditText) Findviewbyid (R.ID.ET_DU); Try{FileInputStream fis=NewFileInputStream (path); byte[] B =New byte[1024]; intI =0; StringBuilder Str=NewStringBuilder (); while((I=fis.read (b)) >0) {str.append (NewString (b,0, i)); } et_du.settext (str); Fis.close (); } Catch(Exception e) {e.printstacktrace (); } } Else{toast.maketext (Sdkacunchu). This, "No SD card found", Toast.length_short). Show (); } }}
Mobile External Storage Exercises