Android Environment 和 Statfs

Android Environment 和 Statfs

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(String type) 
解释:返回 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 public static String DIRECTORY_ALARMS 系统提醒铃声存放的标准目录。
public static String DIRECTORY_DCIM 相机拍摄照片和视频的标准目录。
public static String DIRECTORY_DOWNLOADS 下载的标准目录。
public static String DIRECTORY_MOVIES 电影存放的标准目录。
public static String DIRECTORY_MUSIC 音乐存放的标准目录。
public static String DIRECTORY_NOTIFICATIONS 系统通知铃声存放的标准目录。
public static String DIRECTORY_PICTURES 图片存放的标准目录。
public static String DIRECTORY_PODCASTS 系统广播存放的标准目录。
public static String DIRECTORY_RINGTONES 系统铃声存放的标准目录。


 

public static File getExternalStoragePublicDirectory (String type)
Since:  API Level 8

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

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

 

 

Java代码   收藏代码
  1. void createExternalStoragePublicPicture() {  
  2.     // Create a path where we will place our picture in the user's  
  3.     // public pictures directory.  Note that you should be careful about  
  4.     // what you place here, since the user often manages these files.  For  
  5.     // pictures and other media owned by the application, consider  
  6.     // Context.getExternalMediaDir().  
  7.     File path = Environment.getExternalStoragePublicDirectory(  
  8.             Environment.DIRECTORY_PICTURES);  
  9.     File file = new File(path, "DemoPicture.jpg");  
  10.   
  11.     try {  
  12.         // Make sure the Pictures directory exists.  
  13.         path.mkdirs();  
  14.   
  15.         // Very simple code to copy a picture from the application's  
  16.         // resource into the external file.  Note that this code does  
  17.         // no error checking, and assumes the picture is small (does not  
  18.         // try to copy it in chunks).  Note that if external storage is  
  19.         // not currently mounted this will silently fail.  
  20.         InputStream is = getResources().openRawResource(R.drawable.balloons);  
  21.         OutputStream os = new FileOutputStream(file);  
  22.         byte[] data = new byte[is.available()];  
  23.         is.read(data);  
  24.         os.write(data);  
  25.         is.close();  
  26.         os.close();  
  27.   
  28.         // Tell the media scanner about the new file so that it is // 这一步非常重要!!  
  29.         // immediately available to the user.  
  30.         MediaScannerConnection.scanFile(this,  
  31.                 new String[] { file.toString() }, null,  
  32.                 new MediaScannerConnection.OnScanCompletedListener() {  
  33.             public void onScanCompleted(String path, Uri uri) {  
  34.                 Log.i("ExternalStorage""Scanned " + path + ":");  
  35.                 Log.i("ExternalStorage""-> uri=" + uri);  
  36.             }  
  37.         });  
  38.     } catch (IOException e) {  
  39.         // Unable to create file, likely because external storage is  
  40.         // not currently mounted.  
  41.         Log.w("ExternalStorage""Error writing " + file, e);  
  42.     }  
  43. }  
  44.   
  45. void deleteExternalStoragePublicPicture() {  
  46.     // Create a path where we will place our picture in the user's  
  47.     // public pictures directory and delete the file.  If external  
  48.     // storage is not currently mounted this will fail.  
  49.     File path = Environment.getExternalStoragePublicDirectory(  
  50.             Environment.DIRECTORY_PICTURES);  
  51.     File file = new File(path, "DemoPicture.jpg");  
  52.     file.delete();  
  53. }  
  54.   
  55. boolean hasExternalStoragePublicPicture() {  
  56.     // Create a path where we will place our picture in the user's  
  57.     // public pictures directory and check if the file exists.  If  
  58.     // external storage is not currently mounted this will think the  
  59.     // picture doesn't exist.  
  60.     File path = Environment.getExternalStoragePublicDirectory(  
  61.             Environment.DIRECTORY_PICTURES);  
  62.     File file = new File(path, "DemoPicture.jpg");  
  63.     return file.exists();  
  64. }  

 

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Android系统提供的StatFs类来获取存储空间和剩余存储空间。具体步骤如下: 1. 获取存储器的根目录路径,可以使用Environment类中的getExternalStorageDirectory()方法来获取外部存储路径,或者使用getFilesDir()方法来获取内部存储路径。 2. 创建StatFs对象,需要传入存储器路径。 3. 调用StatFs对象的getBlockSize()方法获取每个块的大小,调用getAvailableBlocks()方法获取可用的块数,调用getBlockCount()方法获取总块数。 4. 计算存储空间和剩余存储空间,可以使用以下公式: 存储空间 = 每个块的大小 * 总块数 / 1024 / 1024 / 1024; 剩余存储空间 = 每个块的大小 * 可用的块数 / 1024 / 1024 / 1024; 以下是示例代码: ```java // 获取存储器路径 File path = Environment.getExternalStorageDirectory(); // 创建StatFs对象 StatFs stat = new StatFs(path.getPath()); // 获取每个块的大小、可用的块数、总块数 long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); long totalBlocks = stat.getBlockCount(); // 计算存储空间和剩余存储空间 long totalSize = blockSize * totalBlocks / 1024 / 1024 / 1024; long availableSize = blockSize * availableBlocks / 1024 / 1024 / 1024; // 输出结果 Log.i("Storage", "Total size: " + totalSize + "GB"); Log.i("Storage", "Available size: " + availableSize + "GB"); ``` 注意:需要添加读取存储空间的权限,即在AndroidManifest.xml文件中添加以下代码: ```xml <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值