一、 当程序通过Context的openFileOutput或openFileInput来打开文件的输入输出流时,程序所打开的都是应用程序的数据文件夹里的文件,但是由于手机的存储空间十分有限,所以我们要采用SD卡上进行存储,这样我们可以大大的扩充手机的存储能力
读写SD卡上的文件的步骤如下:
1、 Environment的getExternalStorageDirectory方法来判断手机上是否插了SD卡,并且应用程序具有读写SD卡的权限。
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
2、调用Environment的getExternalStorageDirectory方法来获得外部存储卡,也就是SD卡的路径
3、使用FileInputStream、FileOutputStream、FileReader和FileWriter读写SD卡的内容
二、应用程序读写SD卡的文件有如下两个注意点:
<!-- 允许用户创建和删除sdcard卡上的内容 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
<!-- 允许用户往sdcard卡上写入的内容 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
三、下面是对SD卡读取的实例:
在AndroidManifest.xml文件中:
1 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 2 package="com.example.android_sdcard" 3 android:versionCode="1" 4 android:versionName="1.0" > 5 6 <uses-sdk 7 android:minSdkVersion="10" 8 android:targetSdkVersion="15" /> 9 10 <!-- 添加读写sdcard卡的授权 --> 11 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 12 13 <instrumentation 14 android:name="android.test.InstrumentationTestRunner" 15 android:targetPackage="com.example.android_sdcard" > 16 </instrumentation> 17 18 <application 19 android:icon="@drawable/ic_launcher" 20 android:label="@string/app_name" 21 android:theme="@style/AppTheme" > 22 <uses-library android:name="android.test.runner"/> 23 <activity 24 android:name=".MainActivity" 25 android:label="@string/title_activity_main" > 26 <intent-filter> 27 <action android:name="android.intent.action.MAIN" /> 28 29 <category android:name="android.intent.category.LAUNCHER" /> 30 </intent-filter> 31 </activity> 32 </application> 33 34 </manifest>
在FileService.java文件中:
1 public class FileService { 2 3 private Context context; 4 5 public FileService(Context context) { 6 this.context = context; 7 } 8 9 public FileService() { 10 11 } 12 13 public String getFileFromSdcard(String fileName) { 14 FileInputStream inputStream = null; 15 // 缓存的流,和磁盘无关,不需要关闭 16 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 17 File file = new File(Environment.getExternalStorageDirectory(), 18 fileName); 19 if (Environment.MEDIA_MOUNTED.equals(Environment 20 .getExternalStorageState())) { 21 try { 22 inputStream = new FileInputStream(file); 23 int len = 0; 24 byte[] data = new byte[1024]; 25 while ((len = inputStream.read(data)) != -1) { 26 outputStream.write(data, 0, len); 27 } 28 29 } catch (FileNotFoundException e) { 30 // TODO Auto-generated catch block 31 e.printStackTrace(); 32 } catch (IOException e) { 33 // TODO Auto-generated catch block 34 e.printStackTrace(); 35 } finally { 36 if (inputStream != null) { 37 try { 38 inputStream.close(); 39 } catch (IOException e) { 40 // TODO Auto-generated catch block 41 e.printStackTrace(); 42 } 43 } 44 } 45 46 } 47 48 return new String(outputStream.toByteArray()); 49 } 50 51 /** 52 * @param fileName 53 * 文件的名称 54 * @param content 55 * 文件的内容 56 * @return 57 */ 58 public boolean saveContentToSdcard(String fileName, String content) { 59 boolean flag = false; 60 FileOutputStream fileOutputStream = null; 61 // 获得sdcard卡所在的路径 62 File file = new File(Environment.getExternalStorageDirectory(), 63 fileName); 64 // 判断sdcard卡是否可用 65 if (Environment.MEDIA_MOUNTED.equals(Environment 66 .getExternalStorageState())) { 67 try { 68 fileOutputStream = new FileOutputStream(file); 69 fileOutputStream.write(content.getBytes()); 70 flag = true; 71 } catch (FileNotFoundException e) { 72 // TODO Auto-generated catch block 73 e.printStackTrace(); 74 } catch (IOException e) { 75 // TODO Auto-generated catch block 76 e.printStackTrace(); 77 } finally { 78 if (fileOutputStream != null) { 79 try { 80 fileOutputStream.close(); 81 } catch (IOException e) { 82 // TODO Auto-generated catch block 83 e.printStackTrace(); 84 } 85 } 86 } 87 } 88 return flag; 89 } 90 }
在MyTest.java文件中:
1 public class MyTest extends AndroidTestCase { 2 3 private final String TAG = "MyTest"; 4 5 public MyTest() { 6 // TODO Auto-generated constructor stub 7 } 8 9 public void saveFile() { 10 11 Context context = getContext(); 12 FileService fileService = new FileService(context); 13 boolean flag = fileService.saveContentToSdcard("hello.txt", "你好"); 14 Log.i(TAG, "-->>" + flag); 15 } 16 17 public void readFile() { 18 Context context = getContext(); 19 FileService fileService = new FileService(context); 20 String msgString = fileService.getFileFromSdcard("hello.txt"); 21 Log.i(TAG, "--->>" + msgString); 22 } 23 }
运行结果:
注意:hello.txt