当前位置:操作系统 > 安卓/Android >>

android 获取手机和SD卡可用内存

import java.io.File;
import android.os.Environment;
import android.os.StatFs;
/*
 * 手机和SD卡内存获取
 *
 * */
public class MemoryManager
{


//获取手机可用的内存空间   返回  单位 M
public float getMemorySize()
{
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return (availableBlocks * blockSize)*1.0f/(1024*1024);
}

//获取手机可用的内存空间 返回  单位 G
public float getSDSize()
{
String state = Environment.getExternalStorageState();
//SD卡不可用
if (!Environment.MEDIA_MOUNTED.equals(state))
{
   return -1;

}

File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return (availableBlocks * blockSize)*1.0f/(1024*1024*1024);
 
}


}

摘自 qq1761310972

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,