获取Android设备的总内存

时间:2017-08-21 06:47:52

标签: java android memory internal-storage

我正在尝试获取Android设备的总内存。但它让我恢复了24GB。但是,设备的总内部存储器为32GB。

我尝试过使用......

public long getInternalFreeSpace()    {
    //Get free Bytes...
    long bytesAvailable = stat.getBlockSizeLong() * stat.getAvailableBlocksLong();
    return bytesAvailable;
}

5 个答案:

答案 0 :(得分:3)

使用此:

public static long getAvailableInternalMemorySize() {
    File path = Environment.getDataDirectory();
    StatFs stat = new StatFs(path.getPath());
    long blockSize = stat.getBlockSize();
    long availableBlocks = stat.getAvailableBlocks();
    long size = availableBlocks * blockSize;
    long availableBlocksInKB = size /= 1024;
    long availableBlocksInMB = availableBlocksInKB /= 1024;
    return availableBlocksInMB;
}

答案 1 :(得分:0)

试试这个:

public long getInternalFreeSpace(){
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
return long bytesAvailable = (long)stat.getBlockSize() *(long)stat.getBlockCount();
}

答案 2 :(得分:0)

阅读 StatFs

  

检索有关文件系统空间的整体信息

private static long getInternalMemorySize() 
 {
    final File pathOBJ = Environment.getDataDirectory();
    final StatFs stat = new StatFs(pathOBJ.getPath());
    final long blockSize;
    final long totalBlocks;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) 
    {
        blockSize = stat.getBlockSizeLong();
        totalBlocks = stat.getBlockCountLong();
    } else 
    {

        blockSize = stat.getBlockSize();
        totalBlocks = stat.getBlockCount();
    }
    return totalBlocks * blockSize;
 }

您可以查看 Get total memory of certain device

答案 3 :(得分:0)

试试这个,让我知道它是否适合你......

StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long bytesAvailable = (long)stat.getBlockSize() *(long)stat.getBlockCount();
long megAvailable = bytesAvailable / 1048576;

答案 4 :(得分:0)

textView.setContentOffset(CGPoint.zero, animated: false)

https://developer.android.com/reference/android/os/StatFs.html#getFreeBlocksLong()