問題小結(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; 
   }
   else{
    notice=notice+"\nSD卡使用情況:未插入SD卡"; 
    return notice; 
   }

     }
}
 作者:agods
 

發佈留言