1.本文主要介绍如何写数据到sd卡,这里主要到的技术是Environment中的方法.
2.实现代码:
/datasave/src/com/amos/datasave/savePasswordService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| //写数据到sdcard public void savePasswordToSDCard(String name, String password) { // android 2.1 /sdcard/xx.txt // android 2.2 /mnt/sdcard/xx.txt // self_define /excard/xx.txt // File externalStorageDirectory = Environment.getExternalStorageDirectory(); // String path = externalStorageDirectory.getPath(); // System.out.println("path:" + path);
// 要存储的内容 String content = name + ":" + password; Log.d(tag, "检验sdcard是否可用?"); //判断sdcard是否存在? if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ Log.d(tag, "sdcard不可用!"); Toast.makeText(context, "没有找到SDCard!", Toast.LENGTH_LONG); return ; }; try { // File file = new File("/sdcard/qqpassword.txt"); // File file = new File(path + "/qqpassword2.txt"); File file = new File(Environment.getExternalStorageDirectory(), "/qqpassword2.txt"); FileOutputStream fos = new FileOutputStream(file); fos.write(content.getBytes()); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); }
}
|
在android2.1及以前版本中,其sdcard目录在根目录,2.2,2.3以后其sdcard目录就变成了/mnt/sdcard了,以及一些厂商自定义的android系统,可能也会把sdcard的名称改的各不相同.这里如果还是用绝对路径,那么程序的兼容性将会大大降低.这里主要用到了Enviroment类.
android.os.Environment其主要方法有:
1 2 3 4 5
| getRootDirectory()---->/ 获取根目录 getDataDirectory()---->/data 获取data目录 getExternalStorageDirectory()--->/sdcard 获取sd卡目录 getDownloadCacheDirectory()--->/cache 获取下载文件的缓存目录 getExternalStorageState--->sdcard的状态,removed,unmounted,checking,nofs,mounted,mounted_ro,shared,unmountable,bad_removal
|
完整的savePasswordService.java文件为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| package com.amos.datasave;
import java.io.File; import java.io.FileOutputStream;
import android.annotation.SuppressLint; import android.content.Context; import android.os.Environment; import android.util.Log; import android.widget.Toast;
@SuppressLint("WorldWriteableFiles") public class savePasswordService { private Context context;
private String tag = "savePasswordService";
public savePasswordService(Context context) { this.context = context; }
public void savePasswordToFile(String name, String password) { String content = name + ":" + password; Log.d(tag, "设置文件的读写权限"); try { FileOutputStream fileOutput = context.openFileOutput("LoginTestConfig.txt", Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE); fileOutput.write(content.getBytes()); fileOutput.flush(); fileOutput.close(); } catch (Exception e) { Log.d(tag, "设置文件的读写权限失败!"); e.printStackTrace(); } } public void savePasswordToSDCard(String name, String password) {
String content = name + ":" + password; Log.d(tag, "检验sdcard是否可用?"); if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ Log.d(tag, "sdcard不可用!"); Toast.makeText(context, "没有找到SDCard!", Toast.LENGTH_LONG); return ; }; try { File file = new File(Environment.getExternalStorageDirectory(), "/qqpassword2.txt"); FileOutputStream fos = new FileOutputStream(file); fos.write(content.getBytes()); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); }
}
}
|
如何获取sdcard的大小?
1 2 3 4 5
| StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getPath()); long blockSize = statFs.getBlockSize(); long blockCount = statFs.getBlockCount(); long sdCardSize = blockSize*blockCount; Log.d(tag,""+sdCardSize );
|
这里使用的是Environment中的方法获取到sdcard的路径,然后将其路径通过StatFs类,该类主要获取指定文件路径下的文件信息(filesystem info).
获取其块大小,块数量.
android 获取当前程序路径
获取当前程序路径
getApplicationContext().getFilesDir().getAbsolutePath();
获取该程序的安装包路径
String path=getApplicationContext().getPackageResourcePath();
获取程序默认数据库路径
getApplicationContext().getDatabasePath(s).getAbsolutePath();