android提供对可移除的外部存储进行文件存储。在对外部sdcard进行调用的时候首先要调用Environment.getExternalStorageState()检查sdcard的可用状态。通过Environment.getExternalStorageDirectory()得到Sdcard的路径。文件写入外部存储需要添加对sdcard的授权
<!-- 写sdcard需要添加写sdcard的授权 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
boolean mExternalStorageAvailable = false; boolean mExternalStorageWriteable = false; String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { // We can read and write the media mExternalStorageAvailable = mExternalStorageWriteable = true; } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { // We can only read the media mExternalStorageAvailable = true; mExternalStorageWriteable = false; } else { // Something else is wrong. It may be one of many other states, but all we need // to know is we can neither read nor write mExternalStorageAvailable = mExternalStorageWriteable = false; }android学习过程中的示例如下: 文件读写工具类:
1 package com.example.android_data_storage_sdcard.file; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 8 import android.os.Environment; 9 import android.util.Log; 10 /** 11 * @author xiaowu 12 * @note 文件存储之外部存储(sdcard存储)。 13 * 外部存储不需要context 14 */ 15 public class FileUitls { 16 private final String TAG = "FileUtil"; 17 18 /** 19 * 保存文件至外部存储的SD卡 20 * 21 * @param fileName 22 * @param mode 23 * @param data 24 * @return 25 */ 26 public boolean saveFileToSdcard(String fileName, byte[] data) { 27 boolean flag = false; 28 // 判断sdcard的状态 29 String state = Environment.getExternalStorageState(); 30 // 获取sdcard的根目录 /mnt/sdcard/... 31 File root = Environment.getExternalStorageDirectory(); 32 FileOutputStream outputStream = null; 33 // Environment.MEDIA_MOUNTED表示SD卡挂载在手机上并可以读写 34 if (Environment.MEDIA_MOUNTED.equals(state)) { 35 // 在sdcard的根目录下创建文件 36 File file = new File(root, fileName); 37 try { 38 outputStream = new FileOutputStream(file); 39 outputStream.write(data, 0, data.length); 40 flag = true ; 41 } catch (FileNotFoundException e) { 42 Log.i(TAG, "文件未找到异常!"); 43 } catch (Exception e) { 44 Log.i(TAG, "写文件发生异常"); 45 } finally { 46 if (outputStream != null) { 47 try { 48 outputStream.close(); 49 } catch (IOException e) { 50 Log.i(TAG, "关闭IO流发生异常!"); 51 } 52 } 53 } 54 } 55 return flag; 56 } 57 }
Junit单元测试类:
单元测试类必须继承自AndroidTestCase,并在资源清单文件中添加单元测试包 <uses-library android:name="android.test.runner"/>,并添加单元测试目标包
<!-- 引入单元测试目标包 -->
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.android_data_storage_sdcard" >
</instrumentation>
1 package com.example.android_data_storage_sdcard; 2 3 import android.test.AndroidTestCase; 4 5 import com.example.android_data_storage_sdcard.file.FileUitls; 6 7 public class MyTest extends AndroidTestCase { 8 9 public void test() { 10 FileUitls fileUitls = new FileUitls(); 11 fileUitls.saveFileToSdcard("keji.txt", "橘子洲头".getBytes()); 12 } 13 }
时间: 2024-10-14 02:10:11