有時,咱們須要將更大的文件保存下來,就不能用手機內置的存儲空間,畢竟是有限的,因此將文件保存在SD卡中。java
要讀寫SD卡,首先要知道手機上是否有SD卡,且是否可讀寫
android
String str = ""; // 判斷是否有SD卡,且可讀 str = Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED) ? "有SD卡,且可讀寫" : "無SD卡或不可讀寫";
上述代碼便可判斷是否有SD卡。app
如今有兩個按鈕:讀、寫dom
兩個EditText : 讀輸入, 寫輸出ide
-----Main.java佈局
public class Main extends Activity { private static final String FILE_NAME = "/myStrongFile.bin"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final EditText text1 = (EditText) findViewById(R.id.editText1); final EditText text2 = (EditText) findViewById(R.id.editText2); Button writeButton = (Button) findViewById(R.id.button1); Button readButton = (Button) findViewById(R.id.button2); String str = ""; // 判斷是否有SD卡,且可讀 str = Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED) ? "有SD卡,且可讀寫" : "無SD卡或不可讀寫"; Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); writeButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { write(text1.getText().toString()); text1.setText(""); } }); readButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { text2.setText(read()); } }); } private String read() { StringBuilder sb = null; if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { // 獲取SD卡對應的存儲目錄 即 /mnt/sdcard/ 路徑 File sdCardDir = Environment.getExternalStorageDirectory(); // 獲取指定文件對應的輸入流 FileInputStream fis; try { fis = new FileInputStream(sdCardDir.getCanonicalPath() + FILE_NAME); BufferedReader reader = new BufferedReader( new InputStreamReader(fis)); sb = new StringBuilder(""); String line = null; while ((line = reader.readLine()) != null) { sb.append(line); } reader.close(); return sb.toString(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return null; } private void write(String str) { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { try { // FileOutputStream(Environment.getExternalStorageDirectory().getCanonicalPath()+FILE_NAME); // 獲取SD卡的目錄 File sdCardDir = Environment.getExternalStorageDirectory(); File targetFile = new File(sdCardDir.getCanonicalPath() + FILE_NAME); // 向指定的文件中添加內容 //此處用RandomAccessFile, 用FileOutputStream會把文件清空 RandomAccessFile raf = new RandomAccessFile(targetFile, "rw"); // 將文件指針指向最後 raf.seek(targetFile.length()); raf.write(str.getBytes()); raf.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } }
----main.xmlui
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="34dp" android:layout_marginTop="20dp" android:ems="10" > <requestFocus /> </EditText> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:layout_below="@+id/editText1" android:layout_marginTop="42dp" android:ems="10" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText2" android:layout_below="@+id/editText2" android:layout_marginTop="44dp" android:text="寫入" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button1" android:layout_alignBottom="@+id/button1" android:layout_alignRight="@+id/editText1" android:layout_marginRight="17dp" android:text="讀取" /> </RelativeLayout>
佈局效果圖:this
運行發現,寫入後,讀不出。指針
原來android讀寫SD卡須要另外添加權限:code
在AndroidManifest.xml中添加權限:
<!-- 在SD卡中建立和刪除文件的權限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- 向SD卡中寫入數據的權限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
再運行就能夠了。此次居然能顯示笑臉了。。。