Android第五个功能:文件存储到SDCard上面
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/filename" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/filename" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/filecontent" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:minLines="3" android:id="@+id/filecontent" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" android:id="@+id/button" /></LinearLayout>
?
package com.dcj.service;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import android.content.Context;import android.os.Environment;public class FileService {private Context context;public FileService(Context applicationContext) {context = applicationContext;}public void saveToSDCard(String filename, String content) throws Exception {File file = new File(Environment.getExternalStorageDirectory(), filename);FileOutputStream outStream = new FileOutputStream(file);outStream.write(content.getBytes());outStream.close();}/** * 保存文件 * @param filename 文件名称 * @param content 文件内容 * @throws Exception */public void save(String filename, String content) throws Exception {// IO j2se//私有操作模式:创建出来的文件只能被本应用访问,其它应用无法访问该文件,另外采用私有操作创建的文件,写入文件会覆盖原文件内容FileOutputStream outStream = context.openFileOutput(filename, context.MODE_PRIVATE);outStream.write(content.getBytes());outStream.close();}/** * 读取文件内容 * @param filename 文件名称 * @return 文件内容 * @throws Exception */public String read(String filename) throws Exception {FileInputStream inStream = context.openFileInput(filename);ByteArrayOutputStream outStream = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int len = 0;while ((len = inStream.read(buffer)) != -1) {outStream.write(buffer, 0, len);}byte[] data = outStream.toByteArray();return new String(data);}}
?
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.dcj" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <uses-library android:name="android.test.runner" /> <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> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.dcj" android:label="Test for my App"/> <!-- 在SDCard中创建与删除文件权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- 往SDCard写入数据权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/></manifest>
?
<?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">Hello World, MainActivity!</string> <string name="app_name">存储到程序内部</string><string name="filename">文件名称</string><string name="filecontent">文件内容</string><string name="button">保存</string><string name="success">保存完成</string><string name="fail">保存失败</string><string name="sdcarderror">SDCard不存在或者写保护</string></resources>
?
package com.dcj;import com.dcj.service.FileService;import android.app.Activity;import android.os.Bundle;import android.os.Environment;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import android.widget.SimpleAdapter.ViewBinder;public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button)this.findViewById(R.id.button); button.setOnClickListener(new ButtonClickListener()); } private final class ButtonClickListener implements View.OnClickListener { public void onClick(View v) { EditText filenameText = (EditText) findViewById(R.id.filename); EditText contentText = (EditText) findViewById(R.id.filecontent); String filename = filenameText.getText().toString(); String content = contentText.getText().toString(); FileService service = new FileService(getApplicationContext()); try {//service.save(filename, content); //换成保存到SDCard上面 //判断SDCard是否存在,并且可以读写 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { service.saveToSDCard(filename, content); Toast.makeText(getApplicationContext(), R.string.success, 1).show(); }else{ Toast.makeText(getApplicationContext(), R.string.sdcarderror, 1).show(); }} catch (Exception e) {Toast.makeText(getApplicationContext(), R.string.fail, 1).show();e.printStackTrace();}} }}
?
?