androidEnvironment .

1、讲述 Environment
Environment 是一个提供访问环境变量的类。


Environment 包含常量:


MEDIA_BAD_REMOVAL
解释:返回getExternalStorageState(),表明SDCard 被卸载前己被移除


MEDIA_CHECKING
解释:返回getExternalStorageState(),表明对象正在磁盘检查。


MEDIA_MOUNTED
解释:返回getExternalStorageState(),表明对象是否存在并具有读/写权限


MEDIA_MOUNTED_READ_ONLY
解释:返回getExternalStorageState(),表明对象权限为只读


MEDIA_NOFS
解释:返回getExternalStorageState(),表明对象为空白或正在使用不受支持的文件系统。


MEDIA_REMOVED
解释:返回getExternalStorageState(),如果不存在 SDCard 返回


MEDIA_SHARED
解释:返回getExternalStorageState(),如果 SDCard 未安装 ,并通过 USB 大容量存储共享 返回

MEDIA_UNMOUNTABLE
解释:返回getExternalStorageState(),返回 SDCard 不可被安装 如果 SDCard 是存在但不可以被安装


MEDIA_UNMOUNTED
解释:返回getExternalStorageState(),返回 SDCard 已卸掉如果SDCard 是存在但是没有被安装


Environment 常用方法:
方法:getDataDirectory()
解释:返回 File ,获取 Android 数据目录。


方法:getDownloadCacheDirectory()
解释:返回 File ,获取 Android 下载/缓存内容目录。


方法:getExternalStorageDirectory()
解释:返回 File ,获取外部存储目录即 SDCard


方法:getExternalStoragePublicDirectory(Stringtype)
解释:返回 File ,取一个高端的公用的外部存储器目录来摆放某些类型的文件


方法:getExternalStorageState()
解释:返回 File ,获取外部存储设备的当前状态


方法:getRootDirectory()
解释:返回 File ,获取 Android 的根目录


2、讲述 StatFs
StatFs 一个模拟linux的df命令的一个类,获得SD卡和手机内存的使用情况


StatFs 常用方法:


getAvailableBlocks()
解释:返回 Int ,获取当前可用的存储空间


getBlockCount()
解释:返回 Int ,获取该区域可用的文件系统数


getBlockSize()
解释:返回 Int ,大小,以字节为单位,一个文件系统


getFreeBlocks()
解释:返回 Int ,该块区域剩余的空间


restat(String path)
解释:执行一个由该对象所引用的文件系统

Constants

String

MEDIA_BAD_REMOVAL

在没有挂载前存储媒体已经被移除。

String

MEDIA_CHECKING

正在检查存储媒体。

String

MEDIA_MOUNTED

存储媒体已经挂载,并且挂载点可读/写。

String

MEDIA_MOUNTED_READ_ONLY

存储媒体已经挂载,挂载点只读。

String

MEDIA_NOFS

存储媒体是空白或是不支持的文件系统。

String

MEDIA_REMOVED

存储媒体被移除。

String

MEDIA_SHARED

存储媒体正在通过USB共享。

String

MEDIA_UNMOUNTABLE

存储媒体无法挂载。

String

MEDIA_UNMOUNTED

存储媒体没有挂载。

Fields

publicstaticString

DIRECTORY_ALARMS

系统提醒铃声存放的标准目录。

public staticString

DIRECTORY_DCIM

相机拍摄照片和视频的标准目录。

public staticString

DIRECTORY_DOWNLOADS

下载的标准目录。

public staticString

DIRECTORY_MOVIES

电影存放的标准目录。

public staticString

DIRECTORY_MUSIC

音乐存放的标准目录。

public staticString

DIRECTORY_NOTIFICATIONS

系统通知铃声存放的标准目录。

public staticString

DIRECTORY_PICTURES

图片存放的标准目录。

public staticString

DIRECTORY_PODCASTS

系统广播存放的标准目录。

public staticString

DIRECTORY_RINGTONES

系统铃声存放的标准目录。

Public Methods

staticFile

getDataDirectory()

获得android data的目录。

staticFile

getDownloadCacheDirectory()

获得下载缓存目录。

staticFile

getExternalStorageDirectory()

或者外部存储媒体目录。

staticFile

getExternalStoragePublicDirectory(Stringtype)

Get a top-level public external storage directory for placing files of a particular type.

staticString

getExternalStorageState()

获得当前外部储存媒体的状态。

staticFile

getRootDirectory()

获得android的跟目录。

publicstaticFilegetExternalStoragePublicDirectory(Stringtype)

Since:API Level 8

Get a top-level public external storage directory forplacing files of a particular type. This is where the user will typically placeand manage their own files, so you should be careful about what you put here toensure you don't erase their files or get in the way of their own organization.

Here is an example of typical code to manipulate apicture on the public external storage:

Java代码

1.voidcreateExternalStoragePublicPicture(){

2.//Createapathwherewewillplaceourpictureintheuser's

3.//publicpicturesdirectory.Notethatyoushouldbecarefulabout

4.//whatyouplacehere,sincetheuseroftenmanagesthesefiles.For

5.//picturesandothermediaownedbytheapplication,consider

6.//Context.getExternalMediaDir().

7.Filepath=Environment.getExternalStoragePublicDirectory(

8.Environment.DIRECTORY_PICTURES);

9.Filefile=newFile(path,"DemoPicture.jpg");

10.

11.try{

12.//MakesurethePicturesdirectoryexists.

13.path.mkdirs();

14.

15.//Verysimplecodetocopyapicturefromtheapplication's

16.//resourceintotheexternalfile.Notethatthiscodedoes

17.//noerrorchecking,andassumesthepictureissmall(doesnot

18.//trytocopyitinchunks).Notethatifexternalstorageis

19.//notcurrentlymountedthiswillsilentlyfail.

20.InputStreamis=getResources().openRawResource(R.drawable.balloons);

21.OutputStreamos=newFileOutputStream(file);

22.byte[]data=newbyte[is.available()];

23.is.read(data);

24.os.write(data);

25.is.close();

26.os.close();

27.

28.//Tellthemediascanneraboutthenewfilesothatitis//这一步非常重要!!

29.//immediatelyavailabletotheuser.

30.MediaScannerConnection.scanFile(this,

31.newString[]{file.toString()},null,

32.newMediaScannerConnection.OnScanCompletedListener(){

33.publicvoidonScanCompleted(Stringpath,Uriuri){

34.Log.i("ExternalStorage","Scanned"+path+":");

35.Log.i("ExternalStorage","->uri="+uri);

36.}

37.});

38.}catch(IOExceptione){

39.//Unabletocreatefile,likelybecauseexternalstorageis

40.//notcurrentlymounted.

41.Log.w("ExternalStorage","Errorwriting"+file,e);

42.}

43.}

44.

45.voiddeleteExternalStoragePublicPicture(){

46.//Createapathwherewewillplaceourpictureintheuser's

47.//publicpicturesdirectoryanddeletethefile.Ifexternal

48.//storageisnotcurrentlymountedthiswillfail.

49.Filepath=Environment.getExternalStoragePublicDirectory(

50.Environment.DIRECTORY_PICTURES);

51.Filefile=newFile(path,"DemoPicture.jpg");

52.file.delete();

53.}

54.

55.booleanhasExternalStoragePublicPicture(){

56.//Createapathwherewewillplaceourpictureintheuser's

57.//publicpicturesdirectoryandcheckifthefileexists.If

58.//externalstorageisnotcurrentlymountedthiswillthinkthe

59.//picturedoesn'texist.

60.Filepath=Environment.getExternalStoragePublicDirectory(

61.Environment.DIRECTORY_PICTURES);

62.Filefile=newFile(path,"DemoPicture.jpg");

63.returnfile.exists();

64.}

Parameters

type

The type of storage directory to return. Should be one ofDIRECTORY_MUSIC,DIRECTORY_PODCASTS,DIRECTORY_RINGTONES,DIRECTORY_ALARMS,DIRECTORY_NOTIFICATIONS,DIRECTORY_PICTURES,DIRECTORY_MOVIES,DIRECTORY_DOWNLOADS, orDIRECTORY_DCIM. May not be null.

Returns

·Returns the File path for the directory. Note that thisdirectory may not yet exist, so you must make sure it exists before using itsuch as withFile.mkdirs().

更多相关文章

  1. android初涉JNI
  2. android避免回复出厂设置导致数据丢失的办法
  3. 解决eclipse中overlaps the location of another project: 'xxxx
  4. Android(安卓)-- 文件扫描
  5. Android(安卓)Studio关于VectorDrawable的兼容
  6. Android按时间先后顺序获取目录下文件列表
  7. 4.28 context.getExternalFilesDir(null).getAbsolutePath()得到
  8. Android(安卓)读取文件内容实现方法总结
  9. Android(安卓)请求Root权限代码

随机推荐

  1. Android使用Https请求服务器,信任所有证书
  2. Android发布项目到JCenter
  3. Android制作update.zip
  4. android 开启新的task
  5. Unity For Android(安卓)iOS 的本地推送
  6. android ViewGroup删除子视图时应该注意
  7. android ndk 编程总结
  8. Android(安卓)7.0 Launcher3的启动和加载
  9. Android(安卓)ViewPager不可滑动
  10. Android网络状态监听及切换,WiFi开关与状