一.特色java
1.把文件存儲在手機外部存儲空間(SD卡)裏android
2.存儲的是任意類型的文件app
3.使用IO輸入輸出流操做文件ide
4.文件路徑工具
1-SD卡根目錄/Android/data/包名/files/[ 文件類型],應用卸載後,數據同時被刪除;this
2-SD卡根目錄/,應用卸載以後,數據不會被同時刪除。spa
5.須要聲明權限3d
1-android.permission.WRITE_EXTERNAL_STORAGE,寫入文件;code
2-MOUNT_UNMOUNT_FILESYSTEMS,建立和刪除文件。orm
二.API
1.Environment
1-SD卡的工具類
2-getExternalStorageState() 獲得SD卡的狀態,Environment.MEDIA_MOUNTED 掛載狀態。
3-getExternalStorageDirectory( ) 獲得SD卡的根目錄File實例
2.context.getExternalFilesDir(type) 獲得帶包名的外部存儲路徑
type 文件類型目錄:
1-null 表示不建立類型子目錄
2-會根據類型建立相應的子目錄
手機外部內存存儲代碼展現:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.hanqi.testapp3.MainActivity" 11 android:orientation="vertical"> 12 13 <EditText 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:hint="輸入..." 17 android:id="@+id/et_1"/> 18 19 <Button 20 android:layout_width="match_parent" 21 android:layout_height="wrap_content" 22 android:text="寫入外部存儲文件" 23 android:onClick="bt6_OnClick"/> 24 25 <Button 26 android:layout_width="match_parent" 27 android:layout_height="wrap_content" 28 android:text="讀取外部存儲文件" 29 android:onClick="bt7_OnClick"/> 30 31 32 </LinearLayout>
1 package com.hanqi.testapp3; 2 3 import android.content.SharedPreferences; 4 import android.content.res.AssetManager; 5 import android.graphics.Bitmap; 6 import android.graphics.BitmapFactory; 7 import android.os.Environment; 8 import android.support.v7.app.AppCompatActivity; 9 import android.os.Bundle; 10 import android.text.BidiFormatter; 11 import android.view.View; 12 import android.widget.EditText; 13 import android.widget.ImageView; 14 import android.widget.TextView; 15 import android.widget.Toast; 16 17 import java.io.File; 18 import java.io.FileInputStream; 19 import java.io.FileOutputStream; 20 import java.io.InputStream; 21 import java.io.PrintStream; 22 23 24 public class MainActivity extends AppCompatActivity { 25 26 TextView tv_1; 27 EditText et_1; 28 ImageView iv_1; 29 30 @Override 31 protected void onCreate(Bundle savedInstanceState) { 32 super.onCreate(savedInstanceState); 33 setContentView(R.layout.activity_main); 34 35 et_1=(EditText)findViewById(R.id.et_1); 36 37 } 38 39 40 //寫入外部存儲文件 41 public void bt6_OnClick(View v) 42 { 43 //1.判斷SD卡是否掛載 44 45 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) 46 { 47 //文本框內容 48 String str=et_1.getText().toString(); 49 50 try { 51 //寫入 52 //1.構造輸出流 53 //1)獲得文件路徑 54 55 //獲得SD卡根目錄 56 //String path=Environment.getExternalStorageDirectory().getCanonicalPath(); 57 58 //獲得包名對應的目錄 59 String path=getExternalFilesDir("Music").getAbsolutePath(); 60 61 Toast.makeText(MainActivity.this, "path="+path, Toast.LENGTH_LONG).show(); 62 //2)構造 63 FileOutputStream fos = new FileOutputStream(path+"/test.txt"); 64 65 PrintStream ps=new PrintStream(fos); 66 67 ps.print(str); 68 69 ps.close(); 70 fos.close(); 71 72 Toast.makeText(MainActivity.this, "寫入外部文件成功", Toast.LENGTH_SHORT).show(); 73 } 74 catch (Exception e) 75 { 76 e.printStackTrace(); 77 Toast.makeText(MainActivity.this, "存儲文件出錯", Toast.LENGTH_SHORT).show(); 78 } 79 } 80 else 81 { 82 Toast.makeText(MainActivity.this, "SD卡沒有掛載", Toast.LENGTH_SHORT).show(); 83 } 84 } 85 86 public void bt7_OnClick(View v) 87 { 88 //1.判斷SD卡是否掛載 89 90 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) 91 { 92 try { 93 94 String path = getExternalFilesDir("Music").getCanonicalPath() + "/test.txt"; 95 96 FileInputStream fis=new FileInputStream(path); 97 98 byte [] b=new byte[1024]; 99 int i=0; 100 101 String str=""; 102 103 while ((i=fis.read(b))>0) 104 { 105 str+=new String(b,0,i); 106 } 107 108 fis.close(); 109 110 Toast.makeText(MainActivity.this, "文件內容="+str, Toast.LENGTH_SHORT).show(); 111 } 112 catch (Exception e) 113 { 114 115 Toast.makeText(MainActivity.this, "讀取外部文件失敗", Toast.LENGTH_SHORT).show(); 116 } 117 } 118 else 119 { 120 Toast.makeText(MainActivity.this, "SD卡沒有掛載", Toast.LENGTH_SHORT).show(); 121 } 122 } 123 }