有時候應(yīng)用需要將數(shù)據(jù)寫入到設(shè)備的外部存儲上。列如,需要同其他應(yīng)用或用戶共享音樂、圖片或者網(wǎng)絡(luò)下載資料時,保存在外部設(shè)備的數(shù)據(jù)共享起來要比較方便。而且,外部設(shè)備通常具有更大的存儲空間。
我們可以通過android.os.Environment.getExternalStorageDirectory()方法獲取sdCard的路徑。再在此路徑下創(chuàng)建一個MyFiles的文件,將數(shù)據(jù)保存在MyFiles文件夾下。
下面就展示如何在外部存儲設(shè)備中存儲和加載本地文件:
1、創(chuàng)建一個名為 DataStorage的工程
2、準(zhǔn)備好布局文件(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="保存數(shù)據(jù)" />
<Button
android:id="@+id/load_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="加載數(shù)據(jù)" />
</LinearLayout>
3、在AndroidManifest.xml中加入往sdCard寫入文件的權(quán)限
<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();
// 獲取外部存儲設(shè)備(SD卡)的路徑
Log.i(TAG, sdCard.getAbsolutePath());
// 查看LogCat,獲取的sd卡的絕對路徑為 /storage/sdcard
sdCard = new File(sdCard, "/MyFiles");
sdCard.mkdirs();// 創(chuàng)建MyFiles目錄(可創(chuàng)建多級目錄)
sdCard = new File(sdCard, FILENAME);
FileOutputStream out = new FileOutputStream(sdCard);
Writer writer = new OutputStreamWriter(out);
try {
String str = "來自保存在內(nèi)部存儲設(shè)備的數(shù)據(jù)";
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("沒有發(fā)現(xiàn)保存的數(shù)據(jù)");
} finally {
reader.close();
}
}
}
運行程序,點擊保存數(shù)據(jù)按鈕,Toast顯示保存成功字樣后。再點擊加載數(shù)據(jù)按鈕:
可以發(fā)現(xiàn)保存在內(nèi)部存儲設(shè)備的設(shè)備被加載后在TextView顯示。再看文件具體位置:
更多建議: