6
如何从应用中检索您的手机内部存储?我发现memoryinfo,但它似乎返回有关您当前正在运行的任务多少内存的信息。我试图让我的应用程序检索有多少内部电话存储可用。android内部手机存储
如何从应用中检索您的手机内部存储?我发现memoryinfo,但它似乎返回有关您当前正在运行的任务多少内存的信息。我试图让我的应用程序检索有多少内部电话存储可用。android内部手机存储
使用android.os.Environment
找到内部目录,然后使用android.os.StatFs
来调用它的Unix statfs
系统调用。从Android设置应用程序无耻stolen:
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return Formatter.formatFileSize(this, availableBlocks * blockSize);
我很难有矿工。所以我想分享一下我的工作代码来节省一些人的时间。
在32GB设备和1GB设备上进行测试。
// Return size is in Megabytes
public class DeviceMemory {
public static long getInternalStorageSpace()
{
StatFs statFs = new StatFs(Environment.getDataDirectory().getAbsolutePath());
//StatFs statFs = new StatFs("/data");
long total = ((long)statFs.getBlockCount() * (long)statFs.getBlockSize())/1048576;
return total;
}
public static long getInternalFreeSpace()
{
StatFs statFs = new StatFs(Environment.getDataDirectory().getAbsolutePath());
//StatFs statFs = new StatFs("/data");
long free = ((long)statFs.getAvailableBlocks() * (long)statFs.getBlockSize())/1048576;
return free;
}
public static long getInternalUsedSpace()
{
StatFs statFs = new StatFs(Environment.getDataDirectory().getAbsolutePath());
//StatFs statFs = new StatFs("/data");
long total = ((long)statFs.getBlockCount() * (long)statFs.getBlockSize())/1048576;
long free = ((long)statFs.getAvailableBlocks() * (long)statFs.getBlockSize())/1048576;
long busy = total - free;
return busy;
}
}