当前位置:操作系统 > 安卓/Android >>

问题小结(7)-sd卡和/data分区使用情况检测

用来获取sd卡和系统/data分区的使用情况,包括:总的空间大小、可用空间、已用空间。

[java]
package com.schogini.dhcp; 
 
import java.io.File; 
 
import android.content.Context; 
import android.os.Environment; 
import android.os.StatFs; 
 
public class CheckStore { 
     private static String notice; 
     
     public static String checkSD(Context contect){ 
          
         File data=Environment.getDataDirectory();  //获得data的路径  
         StatFs data_stat=new StatFs(data.getPath()); //创建StatFs对象  
         long data_blockSize=data_stat.getBlockSize(); //获取block的size  
         float data_totalBlocks=data_stat.getBlockCount();//获取block的个数  
         int data_sizeInMb=(int)(data_blockSize*data_totalBlocks)/1024/1024;//计算总容量  
         long data_availableBlocks=data_stat.getAvailableBlocks(); //获取可用block的个数  
         float data_percent=(int)(data_blockSize*data_availableBlocks)/1024/1024;//计算可用容量  
         notice="FLASH使用情况:\n总容量:"+data_sizeInMb+"M.\n已用:"+(data_sizeInMb-data_percent+"\n可用:"+data_percent+"M."); 
         
         if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ 
                File filePath=Environment.getExternalStorageDirectory();    //获得sd 卡的路径  
                 
                StatFs stat=new StatFs(filePath.getPath());                 //创建StatFs对象  
                long blockSize=stat.getBlockSize();                         //获取block的size  
                float totalBlocks=stat.getBlockCount();                     //获取block的个数  
                int sizeInMb =(int)(blockSize*totalBlocks)/1024/1024;       //计算总容量  
                long availableBlocks=stat.getAvailableBlocks();             //可用block的个数  
                float percent=(int)(blockSize*availableBlocks)/1024/1024;              //计算可用容量  
                 
                notice=notice+"\nSD卡使用情况:\n总容量:"+sizeInMb+"M.\n已用:"+(sizeInMb-percent+"M\n可用:"+percent+"M."); 
                return  notice;  
            } 
            else{ 
                notice=notice+"\nSD卡使用情况:未插入SD卡";    
                return notice;   
            } 
 
     } 

package com.schogini.dhcp;

import java.io.File;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;

public class CheckStore {
  private static String notice;
 
     public static String checkSD(Context contect){
     
      File data=Environment.getDataDirectory(); //获得data的路径
      StatFs data_stat=new StatFs(data.getPath()); //创建StatFs对象
      long data_blockSize=data_stat.getBlockSize(); //获取block的size
      float data_totalBlocks=data_stat.getBlockCount();//获取block的个数
      int data_sizeInMb=(int)(data_blockSize*data_totalBlocks)/1024/1024;//计算总容量
      long data_availableBlocks=data_stat.getAvailableBlocks(); //获取可用block的个数
      float data_percent=(int)(data_blockSize*data_availableBlocks)/1024/1024;//计算可用容量
      notice="FLASH使用情况:\n总容量:"+data_sizeInMb+"M.\n已用:"+(data_sizeInMb-data_percent+"\n可用:"+data_percent+"M.");
     
      if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
    File filePath=Environment.getExternalStorageDirectory();    //获得sd 卡的路径
      
    StatFs stat=new StatFs(filePath.getPath());                 //创建StatFs对象
    long blockSize=stat.getBlockSize();                         //获取block的size
    float totalBlocks=stat.getBlockCount();                     //获取block的个数
    int sizeInMb =(int)(blockSize*totalBlocks)/1024/1024;       //计算总容量
    long availableBlocks=stat.getAvailableBlocks();             //可用block的个数
    float percent=(int)(blockSize*availableBlocks)/1024/1024;              //计算可用容量
    
    notice=notice+"\nSD卡使用情况:\n总容量:"+sizeInMb+"M.\n已用:"+(sizeInMb-percent+"M\n可用:"+percent+"M.");
    return  notice; 
 &

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,