litesuits / litesuits/android-common

建议增加获取存储空间

Open
#18 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
3.6k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

/**
* 存储空间管理
*
*/
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");
}
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file or test is named. Start by locating the Android utility entry points and reviewing the proposed MemorySpaceCheck code, especially its StatFs and Environment usage; done means the project exposes available and total storage values for system and external storage and can check space for a file.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
mobile
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.