读写sd卡中的文件依照例如以下步骤:1调用Environment的getExternalStorageState()方法推断手机上是否插入了sd卡。而且应用程序具有读写SD卡的能力
//假设手机已经插入了SD卡,且具有读写sd卡的能力。以下的语句将会返回true
Environment.getExternalStorageState().equals(Envronment.MEDIA_MOUNTED)
2)调用environment的getExternalStorageDIrectory()方法获取外部存储器,也就是SD卡的文件夹
3)使用FileInputStream、FileOUtputStream FileReader或者FileWriter来读写sd卡中的文件
注冊权限
<--!在SD卡中创建与删除文件权限-->
<uses-permission android:name="android.permission.MOUNT_FORMAT_FILESYSTEMS"/>
<--!向SD卡写入数据的权限--->
<uses-permission android:name="androd.permission.WRITE_EXTERNAL_STORAGE"/>
<--!读取数据的权限--->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<--!向SD卡写入数据的权限--->
<uses-permission android:name="androd.permission.WRITE_EXTERNAL_STORAGE"/>
<--!读取数据的权限--->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
实例代码:
有两个EditText 和两个Button组件,text_Write用于写入数据,Button(write)组件用于读取text_Write中的数据并写入/storage/emulated/0/object.txt 文件里。
Button(read)组件用于从SD卡的/storage/emulated/0/object.txt中读取数据,并显示在text_Read组件中。
<span style="font-size:18px;"><LinearLayout 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" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.sdcard03.MainActivity$PlaceholderFragment" > <EditText android:id="@+id/text_Write" android:layout_width="match_parent" android:layout_height="45dp" /> <EditText android:id="@+id/text_Read" android:layout_width="match_parent" android:layout_height="45dp" /> <Button android:id="@+id/read" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="read"/> <Button android:id="@+id/write" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="write"/> </LinearLayout> </span>主程序:
<span style="font-size:18px;">package com.example.sdcard03; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText read_Text; private EditText write_Text; private Button read; private Button write; private String fileName = <span style="color:#FF6666;">"/object.txt";</span> @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); read = (Button) findViewById(R.id.read); write = (Button) findViewById(R.id.write); read_Text = (EditText) findViewById(R.id.text_Read); write_Text = (EditText) findViewById(R.id.text_Write); read.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String s = readFromSdcard(); read_Text.setText(s); } }); // ------------------------ write.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String s2 = write_Text.getText().toString(); writeToSdcard(s2); } }); } public String readFromSdcard() { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { try { File sdPath = Environment<span style="color:#FF0000;">.getExternalStorageDirectory()</span>; <span style="color:#6633FF;">System.out.println(sdPath.toString());</span> FileInputStream fis = new FileInputStream(sdPath .<span style="color:#FF0000;">getCanonicalFile()</span>.toString() + fileName); <span style="color:#3366FF;">System.out.println(sdPath.getCanonicalFile().toString());</span> BufferedReader br = new BufferedReader(new InputStreamReader( fis)); StringBuilder sb = new StringBuilder(""); String line = null; while ((line = br.readLine()) != null) { sb.append(line); } fis.close(); br.close(); return sb.toString(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } // ------------------------------------------------------- public void writeToSdcard(String s) { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { File sdCardDir = Environment.getExternalStorageDirectory(); try { File file = new File(sdCardDir.getCanonicalPath() + fileName); FileOutputStream fos = new FileOutputStream(file); fos.write(s.getBytes()); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { Toast.makeText(MainActivity.this, "sd卡异常", Toast.LENGTH_LONG) .show(); } } } </span>
几点注意:1 不要忘记注冊权限
2,
<span style="font-size:18px;">File sdPath = Environment<span style="color:#FF0000;">.getExternalStorageDirectory()</span>;中getExternalStorageDirectory()方法返回的是什么? </span><pre name="code" class="java"><span style="font-size:18px;">sdPath.<span style="color:#FF0000;">getCanonicalFile()</span>.toString()方法返回的是什么? 通过上面两行蓝色输出语句能够在logcat中看到返回的都是/storage/emulated/0 注意在0后面没有“/”反斜杠,所以我们在最初声明字符串常量filName时</span><pre name="code" class="java"><span style="font-size:18px;">private String fileName = <span style="color:#FF6666;">"/object.txt";object前有反斜杠,也就是代表了路径</span></span><pre name="code" class="java"><pre name="code" class="java">/storage/emulated/0<span style="color:#FF6666;">/object.txt</span> 。假设我们最初在声明fileName时不加反斜杠就变成了/storage/emulated/0<span style="color:#FF6666;">object.txt</span>。这个路径是不合法的,不存在程序会报错 03-15 15:07:39.440: W/System.err(26730): java.io.FileNotFoundException: /storage/emulated/0object.txt: open failed: ENOENT (No such file or directory) <pre name="code" class="java"><pre name="code" class="java"><pre name="code" class="java">