Android 获取存储卡路径和空间使用情况

/** 获取存储卡路径 */  
 
  File sdcardDir=Environment.getExternalStorageDirectory();   
 
  /** StatFs 看文件系统空间使用情况 */  
 
  StatFs statFs=new StatFs(sdcardDir.getPath());   
 
  /** Block 的 size*/  
 
  Long blockSize=statFs.getBlockSize();   
 
  /** 总 Block 数量 */  
 
  Long totalBlocks=statFs.getBlockCount();   
 
  /** 已使用的 Block 数量 */  
 
  Long availableBlocks=statFs.getAvailableBlocks();

 

public class

Environment 英音:[in'vaiərənmənt]美音:[ɪn'vaɪrənmənt]

extends Object

<!-- end header -->

java.lang.Object
   ↳android.os.Environment

Class Overview(类概述)


Provides access to environment variables. 提供访问环境变量。

 

 

常量

Constants
StringMEDIA_BAD_REMOVAL getExternalStorageState() returns MEDIA_BAD_REMOVAL if the media was removed before it was unmounted.
StringMEDIA_CHECKING getExternalStorageState() returns MEDIA_CHECKING if the media is present and being disk-checked
StringMEDIA_MOUNTED getExternalStorageState() returns MEDIA_MOUNTED if the media is present and mounted at its mount point with read/write access.
StringMEDIA_MOUNTED_READ_ONLY getExternalStorageState() returns MEDIA_MOUNTED_READ_ONLY if the media is present and mounted at its mount point with read only access.
StringMEDIA_NOFS getExternalStorageState() returns MEDIA_NOFS if the media is present but is blank or is using an unsupported filesystem
StringMEDIA_REMOVED getExternalStorageState() returns MEDIA_REMOVED if the media is not present.
StringMEDIA_SHARED getExternalStorageState() returns MEDIA_SHARED if the media is present not mounted, and shared via USB mass storage.
StringMEDIA_UNMOUNTABLE getExternalStorageState() returns MEDIA_UNMOUNTABLE if the media is present but cannot be mounted.
StringMEDIA_UNMOUNTED getExternalStorageState() returns MEDIA_UNMOUNTED if the media is present but not mounted.

 

 

 

 

Public Constructors(构造函数)
  Environment()

<!-- ========== METHOD SUMMARY =========== -->

Public Methods
static File getDataDirectory() :    Gets the Android data directory.获取Android的数据目录
static File getDownloadCacheDirectory()  : Gets the Android Download/Cache content directory.获取的Andr​​oid下载/缓存内容的目录。
static File getExternalStorageDirectory()  :Gets the Android external storage directory.获取Android的外部存储目录。
static File getExternalStoragePublicDirectory(String type) :Get a top-level public external storage directory for placing files of a particular type.获取为特定类型的文件放在顶层的公共外部存储目录。
static String getExternalStorageState() :Gets the current state of the primary "external" storage device.获取的主要“外部”存储设备的当前状态。
static File getRootDirectory() :Gets the Android root directory.获取Android的根目录
static boolean isExternalStorageEmulated() :Returns whether the device has an external storage device which is emulated.返回设备是否有外部存储设备的仿真
static boolean isExternalStorageRemovable() :Returns whether the primary "external" storage device is removable.返回主“外部存储设备是否是可拆卸的。

 

 

public static File getExternalStorageDirectory ()

获取Android的外部存储目录。这个目录可能目前无法访问,如果它已被安装在其计算机上的用户,已被删除从设备,或发生其他一些问题。你可以确定其当前状态 getExternalStorageState() 

注意:不要被这里的“外部”字混淆。这个目录可以更好地被看作媒体/共享存储。这是一个文件系统,可容纳的数据量相对较大,跨所有应用程序共享(不执行的权限)。传统上,这是一个SD卡,但它也可作为内置存储设备,从受保护的内部存储是不同的,可以在计算机上的文件系统安装在实施。

在与多个“外部”存储目录(如既安全应用程序存储和挂载共享存储)设备中,这个目录表示“小学化”的用户将与外部存储。

应用程序不应该直接使用这个顶级目录,以避免污染用户的根命名空间。任何专用应用程序的文件,这些文件应该被放置在返回目录Context.getExternalFilesDir,该系统将采取删除,如果应用程序被卸载。其他共享文件应该被放置在一个由返回目录getExternalStoragePublicDirectory(字符串)

下面是一个典型的代码,例如监察外部存储的状态:

 

BroadcastReceiver mExternalStorageReceiver;
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;

void updateExternalStorageState() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        mExternalStorageAvailable = mExternalStorageWriteable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        mExternalStorageAvailable = true;
        mExternalStorageWriteable = false;
    } else {
        mExternalStorageAvailable = mExternalStorageWriteable = false;
    }
    handleExternalStorageState(mExternalStorageAvailable,
            mExternalStorageWriteable);
}

void startWatchingExternalStorage() {
    mExternalStorageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("test", "Storage: " + intent.getData());
            updateExternalStorageState();
        }
    };
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
    filter.addAction(Intent.ACTION_MEDIA_REMOVED);
    registerReceiver(mExternalStorageReceiver, filter);
    updateExternalStorageState();
}

void stopWatchingExternalStorage() {
    unregisterReceiver(mExternalStorageReceiver);
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值