Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

建议增加获取存储空间 #18

Open
793383996 opened this issue Jul 11, 2017 · 0 comments
Open

建议增加获取存储空间 #18

793383996 opened this issue Jul 11, 2017 · 0 comments

Comments

@793383996
Copy link

/**

  • 存储空间管理

*/
public class MemorySpaceCheck
{

/** 
 * 计算剩余空间 
 * @param path 
 * @return 
 */  
private static long getAvailableSize(String path)  
{  
    StatFs fileStats = new StatFs(path);  
    fileStats.restat(path);  
    return (long) fileStats.getAvailableBlocks() * fileStats.getBlockSize(); // 注意与fileStats.getFreeBlocks()的区别  
}  
  
/** 
 * 计算总空间 
 * @param path 
 * @return 
 */  
private static long getTotalSize(String path)  
{  
    StatFs fileStats = new StatFs(path);  
    fileStats.restat(path);  
    return (long) fileStats.getBlockCount() * fileStats.getBlockSize();  
}  
  
/** 
 * 计算SD卡的剩余空间 
 * @return 剩余空间 
 */  
public static long getSDAvailableSize()  
{  
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))  
    {  
        return getAvailableSize(Environment.getExternalStorageDirectory().toString());  
    }  
      
    return 0;  
}  
  
/** 
 * 计算系统的剩余空间 
 * @return 剩余空间 
 */  
public static long getSystemAvailableSize()  
{  
    // context.getFilesDir().getAbsolutePath();  
    return getAvailableSize("/data");  
}  
  
/** 
 * 是否有足够的空间 
 * @param filePath 文件路径,不是目录的路径 
 * @return 
 */  
public static boolean hasEnoughMemory(String filePath)  
{  
    File file = new File(filePath);  
    long length = file.length();  
    if (filePath.startsWith("/sdcard") || filePath.startsWith("/mnt/sdcard"))  
    {  
        return getSDAvailableSize() > length;  
    }  
    else  
    {  
        return getSystemAvailableSize() > length;  
    }  
      
}  
  
/** 
 * 获取SD卡的总空间 
 * @return 
 */  
public static long getSDTotalSize()  
{  
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))  
    {  
        return getTotalSize(Environment.getExternalStorageDirectory().toString());  
    }  
      
    return 0;  
}  
  
/** 
 * 获取系统可读写的总空间 
 * @return 
 */  
public static long getSysTotalSize()  
{  
    return getTotalSize("/data");  
}  

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant