Class Overview
提供环境变量的访问。
Summary
Constants
MEDIA_BAD_REMOVAL:破坏性移除,感觉是。在未关机状态下拿下SD卡,并不返回这个常量。
MEDIA_CHECKING:正在扫描SD卡,不知道什么情况下是这样的,在手机开机状态下,放入SD卡,并不返回这个常量。
MEDIA_MOUNTED:此状态表示SD卡正常使用,并能read/write。
MEDIA_MOUNTED_READ_ONLY:此状态表示SD卡可以使用,但是只能进行read访问。
MEDIA_NOFS:SD卡能读取,但是不能使用,卡为空白或是系统不能识别的格式。
MEDIA_REMOVED:SD卡已经移除。无法检测到SD卡的存在。
MEDIA_SHARED:SD卡未安装,通过USB使用其他存储器,没测出来。
MEDIA_UNMOUNTABLE:SD卡存在但不能被安装使用。
MEDIA_UNMOUNTED:SD卡存在但没有安装使用。
Fields
DIRECTORY_ALARMS:此文件夹,默认存放系统提示铃声的。(不是常规音乐)
DIRECTORY_DCIM:此文件夹,默认存放系统多媒体的,通过camera获得的东西。
DIRECTORY_DOWNLOADS:此文件夹,默认存放系统下载的东西。
DIRECTORY_MOVIES:此文件夹,默认存放系统的movie。
DIRECTORY_MUSIC:此文件夹,默认存放系统的music。
DIRECTORY_NOTIFICATIONS: 此文件夹,默认存放系统通知铃声的。(不是常规音乐)
DIRECTORY_PICTURES:此文件夹,默认存放系统picture。
DIRECTORY_PODCASTS:此文件夹,广播音乐,好像是电台什么的。(不是常规音乐)
DIRECTORY_RINGTONES:此文件夹,存放来点的铃声。(不是常规音乐)
Public Methods
getDataDirectory():返回系统中的data文件夹,此文件夹包含了安装软件的信息。
getDownloadCacheDirectory():返回系统的cache文件夹,此文件夹包含了下载/缓存的内容。
getExternalStorageDirectory():返回系统的外部存储文件夹,如SD卡。这个方法建议用Context.getgetExternalFilesDir(String type)这个方法来代替,新方法 建立的文件夹如下/mnt/sdcard/Android/data/项目的名称/files/type。并且当应用卸载后,此文件夹也会卸载,方便管理。
getExternalStoragePublicDirectory:返回最上层的公共外部特定类型的文件存储目录。如/mnt/sdcard/type。
getExternalStorageState():返回外部主存储器的状态。Constants中的对象。
getRootDirectory():得到系统的root文件夹。
isExternalStorageEmulated():是否有仿真的外部存储器。
isExternalStorageRemovable():返回boolean,外部存储设备是否可以移动,即是否可以手动移除。
Example
检测外部存储器的状态
//外部存储器是否可见
boolean mExternalStorageAvailable =false;
//外部存储器是否写入
boolean mExternalStorageWriteable =false;
//在使用外部存储器的时候,要先检查外部存储器的状态
void update的ExternalStorageState(){
//获得外部存储器的状态
String state =Environment.getExternalStorageState();
//可以使用
if(Environment.MEDIA_MOUNTED.equals(state)){
mExternalStorageAvailable = mExternalStorageWriteable =true;
}elseif(Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)){
//只有读取权限
mExternalStorageAvailable =true;
mExternalStorageWriteable =false;
}else{
//没有任何权限
mExternalStorageAvailable = mExternalStorageWriteable =false;
}
外部存储器文件的添加删除
//在外部存储器创建一个.jpg文件
void createExternalStoragePublicPicture(){
// Create a path where we will place our picture in the user's
// public pictures directory. Note that you should be careful about
// what you place here, since the user often manages these files. For
// pictures and other media owned by the application, consider
// Context.getExternalMediaDir().
File path =Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File file =newFile(path,"DemoPicture.jpg");
try{
// Make sure the Pictures directory exists.
//mkdirs()会自动检测文件是否存在,包括父文件夹,不存在就会创建。
path.mkdirs();
// Very simple code to copy a picture from the application's
// resource into the external file. Note that this code does
// no error checking, and assumes the picture is small (does not
// try to copy it in chunks). Note that if external storage is
// not currently mounted this will silently fail.
InputStreamis= getResources().openRawResource(R.drawable.balloons);
OutputStream os =newFileOutputStream(file);
byte[] data =newbyte[is.available()];
is.read(data);
os.write(data);
is.close();
os.close();
// Tell the media scanner about the new file so that it is
// immediately available to the user.
//媒体库扫描更新,添加的文件
MediaScannerConnection.scanFile(this,
newString[]{ file.toString()},null,
newMediaScannerConnection.OnScanCompletedListener(){
publicvoid onScanCompleted(String path,Uri uri){
Log.i("ExternalStorage","Scanned "+ path +":");
Log.i("ExternalStorage","-> uri="+ uri);
}
});
}catch(IOException e){
// Unable to create file, likely because external storage is
// not currently mounted.
Log.w("ExternalStorage","Error writing "+ file, e);
}
}
//删除一个外部存储器的文件
void deleteExternalStoragePublicPicture(){
// Create a path where we will place our picture in the user's
// public pictures directory and delete the file. If external
// storage is not currently mounted this will fail.
File path =Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File file =newFile(path,"DemoPicture.jpg");
file.delete();
}
//检测外部存储器中是否有这个文件
boolean hasExternalStoragePublicPicture(){
// Create a path where we will place our picture in the user's
// public pictures directory and check if the file exists. If
// external storage is not currently mounted this will think the
// picture doesn't exist.
File path =Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File file =newFile(path,"DemoPicture.jpg");
return file.exists();
}