资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

andorid storagetotalandfree

1 在Setting应用中,storage 显示total and free code:

成都创新互联公司长期为1000多家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为巫山企业提供专业的成都网站建设、网站设计,巫山网站改版等技术服务。拥有十多年丰富建站经验和众多成功案例,为您定制开发。

final File path = VolumeInfo.getPath();
final long freeBytes = path.getFreeSpace();
final long totalBytes = path.getTotalSpace();
final long usedBytes = totalBytes - freeBytes;

final String used = Formatter.formatFileSize(context, usedBytes);
final String total = Formatter.formatFileSize(context, totalBytes);
2 VolumeInfo.java 是hidden的,只能通过反射得到。代码如下:
try {
    StorageManager sManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
    Class class_StorageManager = StorageManager.class;
    Method method_getVolumes = class_StorageManager.getMethod("getVolumes");

    List volumes = (List) method_getVolumes.invoke(sManager);

    mClassType = Class.forName("android.os.storage.VolumeInfo");

    for (Object volumeInfo : volumes) {

        mGetMethod = mClassType.getDeclaredMethod("getPath");
        File path = (File) mGetMethod.invoke(volumeInfo);
        final long freeBytes = path.getFreeSpace();
        final long totalBytes = path.getTotalSpace();
        final long usedBytes = totalBytes - freeBytes;

        Log.i("cyy1","freeBytes="+Formatter.formatFileSize(context,freeBytes));

        Log.i("cyy1","usedBytes="+Formatter.formatFileSize(context,usedBytes));

         mGetMethod1 = mClassType.getDeclaredMethod("getId");
        String  id = (String) mGetMethod1.invoke(volumeInfo);
        Log.i("cyy1","id="+id);
    }



}catch (Exception e){
    Log.i("cyy1",e.getMessage());
    e.printStackTrace();
}
得到数据:
08-18 17:28:31.087 32148-32148/ I/cyy1: freeBytes=190 MB
08-18 17:28:31.088 32148-32148/ I/cyy1: usedBytes=22.68 GB
08-18 17:28:31.088 32148-32148/ I/cyy1: id=private
08-18 17:28:31.088 32148-32148/ I/cyy1: freeBytes=9.90 MB
08-18 17:28:31.088 32148-32148 I/cyy1: usedBytes=22.84 GB
08-18 17:28:31.088 32148-32148/ I/cyy1: id=emulated
其中emulated 是external stroage的数据。而private 是data的数据

3 可以通过以下code验证:
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSizeLong();
long blockCount = stat.getBlockCountLong();
long availableBlocks = stat.getAvailableBlocksLong();
long avaibByte =   stat.getAvailableBytes();
Log.i("cyy","avaibByte="+Formatter.formatFileSize(context,avaibByte));
long free= stat.getFreeBytes();
Log.i("cyy","free="+Formatter.formatFileSize(context,free));

String totalSize = Formatter.formatFileSize(context, blockCount*blockSize);
String availableSize = Formatter.formatFileSize(context, availableBlocks*blockSize);

Environment.getExternalStorageDirectory() 跟emulated 数据一致;Environment.getDataDirectory()       
和private数据一致。

当前标题:andorid storagetotalandfree
网站URL:http://cdkjz.cn/article/jsgpdj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220