在调用前需要判断是否有写入权限
Environment类提供了比较丰富的方法
static File | getDataDirectory() 获得android data的目录。 |
static File | getDownloadCacheDirectory() 获得下载缓存目录。 |
static File | getExternalStorageDirectory() 或者外部存储媒体目录。 |
static File | getExternalStoragePublicDirectory(String type) Get a top-level public external storage directory for placing files of a particular type. |
static String | getExternalStorageState() 获得当前外部储存媒体的状态。 |
static File | getRootDirectory() 获得android的跟目录。 |
public void saveSDCard(String filename, String filecontent)throws Exception { File file = new File(Environment.getExternalStorageDirectory(),filename); FileOutputStream outStream = new FileOutputStream(file); outStream.write(filecontent.getBytes()); outStream.close(); }
建立文件夹
Environment.getExternalStorageDirectory()getRootDirectory()//获取手机根目录Environment.getExternalStorageDirectory()getExternalStorageDirectory()//获取SD卡根目录String pt="/data/data/"+path+"/temp";//这里一定要获取sd卡的根目录 不然创建文件夹不成功,File destDir = new File(Environment.getExternalStorageDirectory(),pt);if (!destDir.exists()) { destDir.mkdirs();}