SD卡操作【安卓8】

來源:互聯網
上載者:User

標籤:idt   trace   類型   iss   hid   gif   .com   log   ted   

讀寫SD卡

  Context類的openFileInput和openFileOutput方法都是針對應用程式的資料檔案夾進行的檔案操作,由於手機的ROM容量有限,因此這種操作有一定局限性。

  手機的SD卡提供了比ROM更大的儲存容量。更加適合於存取大檔案。

Environment[android.os. Environment]

常量及方法

類型

描述

public static final String MEDIA_MOUNTED

常量

擴充儲存設允許進行讀寫訪問

public static final String MEDIA_CHECKING

常量

擴充儲存設處於檢查狀態

public static final String MEDIA_MOUNTED_READ_ONLY

常量

擴充儲存設處於唯讀狀態

public static final String MEDIA_REMOVED

常量

擴充儲存設不存在

public static final String MEDIA_UNMOUNTED

常量

沒有找到擴充儲存設

public static File getDataDirectory()

普通

取得Data目錄

public static File getDownloadCacheDirectory()

普通

取得下載的緩衝目錄

public static File getExternalStorageDirectory()

普通

取得擴充的儲存目錄

public static String getExternalStorageState()

普通

取得擴充存放裝置的狀態

public static File getRootDirectory()

普通

取得Root目錄

public static boolean isExternalStorageRemovable()

普通

判斷擴充的儲存目錄是否被刪除

               申請SD卡的讀寫權限

  開啟項目資訊清單檔,在Permission選項卡中選擇許可權

SD卡檔案讀取操作
 1 private TextView tv;     2 private Button bt1; 3 private Button bt2;      4 private EditText et; 5  6 protected void onCreate(Bundle savedInstanceState) { 7         super.onCreate(savedInstanceState); 8         setContentView(R.layout.xml檔案); 9         et = (EditText) findViewById(R.id.sdET);10         tv = (TextView) findViewById(R.id.sdTV);11         bt1 = (Button) findViewById(R.id.sdBT01);12         bt2 = (Button) findViewById(R.id.sdBT02);13         bt1.setOnClickListener(this);14         bt2.setOnClickListener(this);15     }16 17 public void onClick(View v) {18         if (!Environment.getExternalStorageState().equals(19                 Environment.MEDIA_MOUNTED)) {// 判斷SD卡是否存在20             Toast.makeText(Sd0.this, "SD卡不存在", 3000).show();21             return;22         }23         switch (v.getId()) {24         case R.id.sdBT01:25             // 建立檔案夾對象26             File dir = new File(Environment.getExternalStorageDirectory(),"test");27             if (!dir.exists()) { // 如果目錄不存在,就建立28                 dir.mkdir();29             }30             File file = new File(dir, "test.txt"); // 建立檔案31             PrintWriter pw;32             try {33                 pw = new PrintWriter(file);34                 pw.println(et.getText().toString());35                 pw.close();36                 Toast.makeText(Sd0.this, "儲存成功", 3000).show();37             } catch (FileNotFoundException e) {38                 e.printStackTrace();39             }40             break;41         case R.id.sdBT02:42             File f = new File(Environment.getExternalStorageDirectory()+43                     "/test/test.txt");// 建立檔案夾對象44             BufferedReader br;45             try {46                 br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));47                 String str = null;48                 while ((str = br.readLine()) != null) {49                     tv.append(str);50                 }51             } catch (FileNotFoundException e) {52                 e.printStackTrace();53             } catch (IOException e) {54                 e.printStackTrace();55             }56             break;57         }58 }
View Code

 

SD卡操作【安卓8】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.