Logcat
等级
- verbose:冗余,最低等级
- debug:调试
- info:正常等级的信息
- warn:警告
- error:错误
Android的存储
内部存储空间
- RAM内存:运行内存,相当于电脑的内存
- ROM内存:存储内存,相当于电脑的硬盘
//获得data下的应用文件目录
File file = new File(getFilesDir(), "info.txt");
//获得data下的应用缓存目录
File file = new File(getCacheDir(), "info.txt");
外部存储空间
SD卡:相当于电脑的移动硬盘
得到sd目录代码
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File path = Environment.getExternalStorageDirectory();
File file = new File(path+"/info.txt");
}
- 2.2之前,sd卡路径:sdcard
- 4.3之前,sd卡路径:mnt/sdcard
- 4.3开始,sd卡路径:storage/sdcard
得到sd应用目录
//缓存目录
File file = new File(getExternalCacheDir(), "info.txt");
//文件目录
File file = new File(getExternalFilesDir(), "info.txt");
sd总大小和可用量的计算
- 所有存储设备,都会被划分成若干个区块,每个区块有固定的大小
- 存储设备的总大小 = 区块大小 * 区块数量
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize;
long totalBlocks;
long availableBlocks;
//获取当前系统版本的等级
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
blockSize = stat.getBlockSizeLong();
totalBlocks = stat.getBlockCountLong();
availableBlocks = stat.getAvailableBlocksLong();
}
else{
blockSize = stat.getBlockSize();
totalBlocks = stat.getBlockCount();
availableBlocks = stat.getAvailableBlocks();
}
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText(formatSize(availableBlocks * blockSize));
}
private String formatSize(long size) {
return Formatter.formatFileSize(this, size);
}
网友评论