有时候应用需要将数据写入到设备的外部存储上。列如,需要同其他应用或用户共享音乐、图片或者网络下载资料时,保存在外部设备的数据共享起来要比较方便。而且,外部设备通常具有更大的存储空间。
我们可以通过android.os.Environment.getExternalStorageDirectory()方法获取sdCard的路径。再在此路径下创建一个MyFiles的文件,将数据保存在MyFiles文件夹下。
下面就展示如何在外部存储设备中存储和加载本地文件:
1、创建一个名为 DataStorage的工程
2、准备好布局文件(activity_data_storage.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/data_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="未操作" /> <Button android:id="@+id/save_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="保存数据" /> <Button android:id="@+id/load_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="加载数据" /> </LinearLayout>
3、在AndroidManifest.xml中加入往sdCard写入文件的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
4、DataStorageActivity.java
package com.example.datastorage; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Writer; import android.os.Bundle; import android.os.Environment; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class DataStorageActivity extends ActionBarActivity { private static final String FILENAME = "data.txt"; private static final String TAG = "DataStorageActivity"; private TextView dataView; private Button saveButton; private Button loadButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_date_storage); dataView = (TextView) findViewById(R.id.data_view); saveButton = (Button) findViewById(R.id.save_button); loadButton = (Button) findViewById(R.id.load_button); setListener(); } private void setListener() { saveButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { try { saveData(); } catch (IOException e) { } Toast.makeText(DataStorageActivity.this, "保存成功", Toast.LENGTH_SHORT).show(); } }); loadButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { try { loadData(); } catch (FileNotFoundException e) { } catch (IOException e) { } } }); } public void saveData() throws IOException { File sdCard = Environment.getExternalStorageDirectory(); // 获取外部存储设备(SD卡)的路径 Log.i(TAG, sdCard.getAbsolutePath()); // 查看LogCat,获取的sd卡的绝对路径为 /storage/sdcard sdCard = new File(sdCard, "/MyFiles"); sdCard.mkdirs();// 创建MyFiles目录(可创建多级目录) sdCard = new File(sdCard, FILENAME); FileOutputStream out = new FileOutputStream(sdCard); Writer writer = new OutputStreamWriter(out); try { String str = "来自保存在内部存储设备的数据"; writer.write(str); } finally { writer.close(); } } public void loadData() throws FileNotFoundException, IOException { BufferedReader reader = null; StringBuilder data = new StringBuilder(); try { File sdCard = Environment.getExternalStorageDirectory(); sdCard = new File(sdCard, "/MyFiles/" + FILENAME); FileInputStream in = new FileInputStream(sdCard); reader = new BufferedReader(new InputStreamReader(in)); String line = new String(); while ((line = reader.readLine()) != null) { data.append(line); } dataView.setText(data); } catch (FileNotFoundException e) { dataView.setText("没有发现保存的数据"); } finally { reader.close(); } } }
可以发现保存在内部存储设备的设备被加载后在TextView显示。再看文件具体位置:
#DONE#
原文地址:http://blog.csdn.net/u012964281/article/details/39579253