Android 数据存储(四)之sd卡存储

来源:互联网 发布:淘宝网顾老师太极鞋 编辑:程序博客网 时间:2024/05/17 22:29

在操作sd卡之前,一定要确认sd是否存在,并且是可读写的。使用这个判断Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)来处理。
要操作sd卡,必须先添加操作权限:

<!-- 在SDCard中创建与删除文件权限 --><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /><!-- 往SDCard中写入数据权限 --><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

1.创建工程SDSave,修改主布局文件:

<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"    android:orientation="vertical" >    <EditText        android:id="@+id/data"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <Button        android:id="@+id/save"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="save" />    <Button        android:id="@+id/read"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="read" /></LinearLayout>

2.在程序添加对于的代码

public class MainActivity extends Activity {    private EditText mDataET;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mDataET = (EditText) findViewById(R.id.data);        Button save = (Button) findViewById(R.id.save);        save.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                if (Environment.getExternalStorageState().equals(                        Environment.MEDIA_MOUNTED)) {                    String data = mDataET.getText().toString().trim();                    mDataET.setText("");                    File dir = Environment.getExternalStorageDirectory();                    Log.i("info", "dir: " + dir.toString());                    String path = dir.getAbsolutePath();                    Log.i("info", "path: " + path);                    File file = new File(path + File.separator + "data.txt");                    try {                        FileOutputStream fos = new FileOutputStream(file);                        fos.write(data.getBytes());                        fos.flush();                        fos.close();                    } catch (FileNotFoundException e) {                        // TODO Auto-generated catch block                        e.printStackTrace();                    } catch (IOException e) {                        // TODO Auto-generated catch block                        e.printStackTrace();                    }                }            }        });        Button read = (Button) findViewById(R.id.read);        read.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                if (Environment.getExternalStorageState().equals(                        Environment.MEDIA_MOUNTED)) {                    File dir = Environment.getExternalStorageDirectory();                    Log.i("info", "dir: " + dir.toString());                    String path = dir.getAbsolutePath();                    Log.i("info", "path: " + path);                    File file = new File(path + File.separator + "data.txt");                    try {                        FileInputStream fis = new FileInputStream(file);                        BufferedReader reader = new BufferedReader(                                new InputStreamReader(fis));                        StringBuffer data = new StringBuffer();                        String line = "";                        while ((line = reader.readLine()) != null) {                            data.append(line);                        }                        mDataET.setText(data.toString());                        reader.close();                        fis.close();                    } catch (FileNotFoundException e) {                        // TODO Auto-generated catch block                        e.printStackTrace();                    } catch (IOException e) {                        // TODO Auto-generated catch block                        e.printStackTrace();                    }                }            }        });    }}

3.在编辑框输入内容,点击保存,在/mnt/sdcard文件夹下面就可以看到我们保存的数据了。

0 0