WEB开发网
开发学院手机开发Android 开发 获取Android手机中SD卡内存信息 阅读

获取Android手机中SD卡内存信息

 2012-06-18 15:21:14 来源:WEB开发网   
核心提示:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>btn.setOnClickListener(new View.OnClickListene
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
btn.setOnClickListener(new View.OnClickListener() {

            
            @Override
            public void onClick(View v) {
                if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                    //取得sdcard文件路径
                    File path = Environment.getExternalStorageDirectory(); // "mnt/sdcard"
                    path = new File("mnt/extsd");
                    Log.i("chens", "======path========"+path);
                    StatFs statfs = new StatFs(path.getPath());
                    
                    //获取block的SIZE
                    long blocSize = statfs.getBlockSize();
                    
                    //获取BLOCK数量
                    long totalBlocks = statfs.getBlockCount();
                    
                    //空闲的Block的数量
                    long availaBlock = statfs.getAvailableBlocks();
                    
                    //计算总空间大小和空闲的空间大小
                    String[] total = filesize(totalBlocks * blocSize);
                    String[] availale = filesize(availaBlock * blocSize);
                    
                    Toast.makeText(FirstActivity.this, "sdcard总空间大小="+total[0]+total[1]
                            +"\n剩余空间大小="+availale[0]+availale[1], 1).show();
                }else {
                    Toast.makeText(FirstActivity.this, "没有sdcard或者sdcard挂载失败", 1).show();
                }
                
                
                
                

            }
        });
    }
//计算空间方法csize
    String[] filesize(long size) { 
        String str = ""; 
        if (size >= 1024) { 
            str = "KB"; 
            size /= 1024; 
            if (size >= 1024) { 
                str = "MB"; 
                size /= 1024; 
            } 
        }
        DecimalFormat formatter = new DecimalFormat(); 
        formatter.setGroupingSize(3); 
        String result[] = new String[2]; 
        result[0] = formatter.format(size); 
        result[1] = str; 
        return result; 
    }

Tags:获取 Android 手机

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接