获取内存上的可用空间

发布于 2024-10-10 00:09:16 字数 76 浏览 2 评论 0原文

是否可以通过 Android SDK 获取 Android 设备(而不是 SD 卡)上的可用内存量?

如果是这样,怎么办?

Is it possible to get the amount of free memory on an Android device (not on the SD CARD) via the Android SDK?

If so, how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

旧话新听 2024-10-17 00:09:16

这篇文章可能很适合您的问题。

另请检查此线程。这里有很多关于SO的信息。

谷歌搜索了一下,这里是解决方案(位于 android git)

File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return Formatter.formatFileSize(this, availableBlocks * blockSize);

this post might fit well to your question.

also check this thread. there is so much info here on SO.

googled a bit and here is the solution (found at android git)

File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return Formatter.formatFileSize(this, availableBlocks * blockSize);
煞人兵器 2024-10-17 00:09:16
/*************************************************************************************************
Returns size in bytes.

If you need calculate external memory, change this: 
    StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
to this: 
    StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());        
**************************************************************************************************/
    public long TotalMemory()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());   
        long   Total  = ( (long) statFs.getBlockCount() * (long) statFs.getBlockSize());
        return Total;
    }

    public long FreeMemory()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
        long   Free   = (statFs.getAvailableBlocks() * (long) statFs.getBlockSize());
        return Free;
    }

    public long BusyMemory()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());   
        long   Total  = ((long) statFs.getBlockCount() * (long) statFs.getBlockSize());
        long   Free   = (statFs.getAvailableBlocks()   * (long) statFs.getBlockSize());
        long   Busy   = Total - Free;
        return Busy;
    }

将字节转换为人类可读格式(如 1 Mb、1 Gb)

    public static String floatForm (double d)
    {
       return new DecimalFormat("#.##").format(d);
    }


    public static String bytesToHuman (long size)
    {
        long Kb = 1  * 1024;
        long Mb = Kb * 1024;
        long Gb = Mb * 1024;
        long Tb = Gb * 1024;
        long Pb = Tb * 1024;
        long Eb = Pb * 1024;

        if (size <  Kb)                 return floatForm(        size     ) + " byte";
        if (size >= Kb && size < Mb)    return floatForm((double)size / Kb) + " Kb";
        if (size >= Mb && size < Gb)    return floatForm((double)size / Mb) + " Mb";
        if (size >= Gb && size < Tb)    return floatForm((double)size / Gb) + " Gb";
        if (size >= Tb && size < Pb)    return floatForm((double)size / Tb) + " Tb";
        if (size >= Pb && size < Eb)    return floatForm((double)size / Pb) + " Pb";
        if (size >= Eb)                 return floatForm((double)size / Eb) + " Eb";

        return "???";
    }
/*************************************************************************************************
Returns size in bytes.

If you need calculate external memory, change this: 
    StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
to this: 
    StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());        
**************************************************************************************************/
    public long TotalMemory()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());   
        long   Total  = ( (long) statFs.getBlockCount() * (long) statFs.getBlockSize());
        return Total;
    }

    public long FreeMemory()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
        long   Free   = (statFs.getAvailableBlocks() * (long) statFs.getBlockSize());
        return Free;
    }

    public long BusyMemory()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());   
        long   Total  = ((long) statFs.getBlockCount() * (long) statFs.getBlockSize());
        long   Free   = (statFs.getAvailableBlocks()   * (long) statFs.getBlockSize());
        long   Busy   = Total - Free;
        return Busy;
    }

Converting bytes to human readable format (like 1 Mb, 1 Gb)

    public static String floatForm (double d)
    {
       return new DecimalFormat("#.##").format(d);
    }


    public static String bytesToHuman (long size)
    {
        long Kb = 1  * 1024;
        long Mb = Kb * 1024;
        long Gb = Mb * 1024;
        long Tb = Gb * 1024;
        long Pb = Tb * 1024;
        long Eb = Pb * 1024;

        if (size <  Kb)                 return floatForm(        size     ) + " byte";
        if (size >= Kb && size < Mb)    return floatForm((double)size / Kb) + " Kb";
        if (size >= Mb && size < Gb)    return floatForm((double)size / Mb) + " Mb";
        if (size >= Gb && size < Tb)    return floatForm((double)size / Gb) + " Gb";
        if (size >= Tb && size < Pb)    return floatForm((double)size / Tb) + " Tb";
        if (size >= Pb && size < Eb)    return floatForm((double)size / Pb) + " Pb";
        if (size >= Eb)                 return floatForm((double)size / Eb) + " Eb";

        return "???";
    }
坠似风落 2024-10-17 00:09:16

看起来 StatFs 类可能是你需要使用什么。我不确定哪个路径将被视为设备的根目录,但我相信无论目录如何,只要它是内部存储的一部分,结果都是相同的。像这样的东西可能会起作用:

StatFs stats = new StatFs("/data");
int availableBlocks = stats.getAvailableBlocks();
int blockSizeInBytes = stats.getBlockSize();
int freeSpaceInBytes = availableBlocks * blockSizeInBytes;

如果不出意外的话,StatFs 类应该给你一个良好的开始,让你知道去哪里寻找。

It looks like the StatFs class might be what you need to use. I'm not sure what path would be considered the root of the device, but I believe the result would be the same regardless of directory, as long as it's part of the internal storage. Something like this may work:

StatFs stats = new StatFs("/data");
int availableBlocks = stats.getAvailableBlocks();
int blockSizeInBytes = stats.getBlockSize();
int freeSpaceInBytes = availableBlocks * blockSizeInBytes;

If nothing else, the StatFs class should give you a good start on where to look.

星光不落少年眉 2024-10-17 00:09:16

自 2013 年以来,有些方法已被 Google 弃用,您可以更改这些方法(仅限 API 18+):

  • getAvailableBlocks() 更改为 getAvailableBlocksLong()
  • getBlockCount( )getBlockCountLong()
  • getBlockSize()getBlockSizeLong()
  • getFreeBlocks()getFreeBlocksLong()

There are some methods which were deprecated by Google since 2013 and you might change these methods (API 18+ only):

  • getAvailableBlocks() to getAvailableBlocksLong()
  • getBlockCount() to getBlockCountLong()
  • getBlockSize() to getBlockSizeLong()
  • getFreeBlocks() to getFreeBlocksLong()
真心难拥有 2024-10-17 00:09:16

在内存非常大的设备上,它不起作用,因为 int 值太小。例如,在 Motorola xum 上它不起作用。你必须使用这样的东西:

int freeSpaceInKilobytes = availableBlocks * (blockSizeInBytes / 1024);

On devices where internal memory size is very big it doesn't work because int value is too small. For example on Motorola xum it doesn't work. You have to use something like this:

int freeSpaceInKilobytes = availableBlocks * (blockSizeInBytes / 1024);

多孤肩上扛 2024-10-17 00:09:16
/**
 * @return Number of bytes available on internal storage
 */
public static long getInternalAvailableSpace() {
    long availableSpace = -1L;
    try {StatFs stat = new StatFs(Environment.getDataDirectory()
            .getPath());
        stat.restat(Environment.getDataDirectory().getPath());
        availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return availableSpace;
}
/**
 * @return Number of bytes available on internal storage
 */
public static long getInternalAvailableSpace() {
    long availableSpace = -1L;
    try {StatFs stat = new StatFs(Environment.getDataDirectory()
            .getPath());
        stat.restat(Environment.getDataDirectory().getPath());
        availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return availableSpace;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文