Android儲存和載入本地檔案(外部存放裝置),android存放裝置

來源:互聯網
上載者:User

Android儲存和載入本地檔案(外部存放裝置),android存放裝置

       有時候應用需要將資料寫入到裝置的外部儲存上。列如,需要同其他應用或使用者共用音樂、圖片或者網路下載資料時,儲存在外部裝置的資料共用起來要比較方便。而且,外部裝置通常具有更大的儲存空間。

       我們可以通過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;@Overrideprotected 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/sdcardsdCard = 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();}}}


運行程式,點擊儲存資料按鈕,Toast顯示儲存成功字樣後。再點擊載入資料按鈕:



可以發現儲存在內部存放裝置的裝置被載入後在TextView顯示。再看檔案具體位置:





#DONE#



Android的“檔案管理工具”,是怎獲得“本機存放區”與“外置儲存”路徑的?

外部儲存路徑(即sdcard):Environment.getExternalStorageDirectory().getPath();

可以列出所有的目錄:

Environment.getRootDirectory().getParent()
 
安卓手機提示沒有可用的外部儲存,怎辦?

那買個卡,放裡面不就行了,手機儲存不大而且是用來安裝軟體的,不是給你存照片和別的音樂之類的東西,這一般都附送一個的
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.