将数据保存的SDCard前需要先声明权限
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.filesavetosdcard" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" 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> <!-- 在SDCard中创建和删除文件权限--> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- 往SDCard写入数据的权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>" </manifest>
布局文件
<RelativeLayout 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.file.MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="文件名称"/> <EditText android:id="@+id/fileName" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/textView1"/> <TextView android:id="@+id/textView2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/fileName" android:text="文本内容"/> <!-- 输入yu最少显示3行 --> <EditText android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/textView2" android:minLines="3"/> <Button android:id="@+id/save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/content" android:text="保存"/> <TextView android:id="@+id/fileContent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/save" android:text="您输入的内容:"/> </RelativeLayout>
业务逻辑
package com.example.service; import java.io.File; import java.io.FileOutputStream; import android.content.Context; import android.os.Environment; public class FileService { private Context context; public FileService(Context context){ this.context = context; } public void saveToSDCard(String fileName,String content){ /* * 第一个参数:文件保存的路径,即SDCard的路径 * 第二个参数:保存的文件名 */ // File file = new File("/mnt/sdcard",fileName); File file = new File(Environment.getExternalStorageDirectory(),fileName); //SDCard的文件会保存在mnt目录的sdcard文件夹下。SDCard中的数据不受权限控制,可以被任何应用访问 //最好不要用SDCard的绝对路径/sdcard,可能会随版本的变化不同 FileOutputStream outStream; try { outStream = new FileOutputStream(file); outStream.write(content.getBytes()); outStream.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package com.example.filesavetosdcard; import com.example.service.FileService; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private EditText fileName; private EditText content; private Button save; private TextView fileContent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fileName = (EditText) findViewById(R.id.fileName); content = (EditText) findViewById(R.id.content); save = (Button) findViewById(R.id.save); fileContent = (TextView) findViewById(R.id.fileContent); save.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub FileService service = new FileService(getApplicationContext()); //如果手机没有SDCard或SDCard设置了保护权限,我们是无法访问 //SDCard的,所以在操作前应先判断SDCard的状态 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ service.saveToSDCard(fileName.getText().toString(),content.getText().toString()); Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT).show(); fileContent.setText("您输入的内容:" + content.getText().toString()); }else{ Toast.makeText(getApplicationContext(), "SDCard不存在或写保护", Toast.LENGTH_SHORT).show(); } } }); } }
点击保存,数据以文件形式保存在sdcard目录下
将myfile导出以记事本方式打开
时间: 2024-11-15 11:53:02