把文件存入SD卡中
首先加入读写权限
SD卡创建与删除文件权限
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
SD卡写入数据权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
http://schemas.android.com/apk/res/android
"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout xmlns:android="
http://schemas.android.com/apk/res/android
"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="90px"
android:layout_height="wrap_content"
android:text="文件名称:"
android:textSize="21px"
android:id="@+id/filenamelebal"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/filenamelebal"
android:layout_alignTop="@id/filenamelebal"
android:id="@+id/filename"
></EditText>
></RelativeLayout>
<TextView
android:layout_width="90px"
android:layout_height="wrap_content"
android:text="内容:"
android:textSize="21px"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minLines="3"
android:id="@+id/content"
></EditText>
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content">
<Button
android:layout_height="wrap_content"
android:id="@+id/baocun"
android:text="保存"
android:layout_width="wrap_content"></Button>
<Button
android:text="读取"
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button
android:text="退出"
android:id="@+id/exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
<TextView
android:text="读取内容显示"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
http://schemas.android.com/apk/res/android
"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout xmlns:android="
http://schemas.android.com/apk/res/android
"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="90px"
android:layout_height="wrap_content"
android:text="文件名称:"
android:textSize="21px"
android:id="@+id/filenamelebal"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/filenamelebal"
android:layout_alignTop="@id/filenamelebal"
android:id="@+id/filename"
></EditText>
></RelativeLayout>
<TextView
android:layout_width="90px"
android:layout_height="wrap_content"
android:text="内容:"
android:textSize="21px"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minLines="3"
android:id="@+id/content"
></EditText>
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content">
<Button
android:layout_height="wrap_content"
android:id="@+id/baocun"
android:text="保存"
android:layout_width="wrap_content"></Button>
<Button
android:text="读取"
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button
android:text="退出"
android:id="@+id/exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
<TextView
android:text="读取内容显示"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
配置文件
[java]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="
http://schemas.android.com/apk/res/android
"
package="com.luku.file"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="android.test.runner"></uses-library>
<activity android:name=".fileActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.luku.file"
android:label="Test for my app"
></instrumentation>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="
http://schemas.android.com/apk/res/android
"
package="com.luku.file"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="android.test.runner"></uses-library>
<activity android:name=".fileActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.luku.file"
android:label="Test for my app"
></instrumentation>
</manifest>
摘自 奔跑的蜗牛