Mobile External Storage Exercises

Source: Internet
Author: User

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

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.