sd卡状态、路径、可用空间byte,参考http://ai4work.blog.163.com/blog/static/189873306201182310304586/

内存,参考http://blog.csdn.net/hudashi/article/details/7050897

manifest权限:

<!-- 在SDCard中创建与删除文件权限 --><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/><!-- 往SDCard写入数据权限 --><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


code:

package com.test;import android.app.Activity;import android.app.ActivityManager;import android.os.Bundle;import android.os.Environment;import android.os.StatFs;import android.util.Log;public class Test2 extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);int i = 0;while (i < 1) {try {System.out.println("1: " + checkSDCard());String path = getExternalStoragePath();System.out.println("2: " + path);if (path != null) {System.out.println("3: " + (getAvailableStore(path)>>20) + "MB");}displayBriefMemory();} catch (Exception e) {e.printStackTrace();}i++;}}//内存相关private void displayBriefMemory() {            final ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);            ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();           activityManager.getMemoryInfo(info);            Log.i("System.out", "系统剩余内存:"+(info.availMem >> 20)+"MB");           Log.i("System.out", "系统是否处于低内存运行:"+info.lowMemory);        Log.i("System.out", "当系统剩余内存低于" + (info.threshold >> 20) + "MB时就看成低内存运行");    } // 检查SD卡状态private boolean checkSDCard() {if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {return true;}return false;}// 获取SD卡路径public static String getExternalStoragePath() {// 获取SdCard状态String state = android.os.Environment.getExternalStorageState();// 判断SdCard是否存在并且是可用的if (android.os.Environment.MEDIA_MOUNTED.equals(state)) {if (android.os.Environment.getExternalStorageDirectory().canWrite()) {return android.os.Environment.getExternalStorageDirectory().getPath();}}return null;}/** *   * 获取存储卡的剩余容量,单位为字节 *  *   * @param filePath *  *   * @return availableSpare *  *    */public static long getAvailableStore(String filePath) {// 取得sdcard文件路径StatFs statFs = new StatFs(filePath);// 获取block的SIZElong blocSize = statFs.getBlockSize();// 获取BLOCK数量// long totalBlocks = statFs.getBlockCount();// 可使用的Block的数量long availaBlock = statFs.getAvailableBlocks();// long total = totalBlocks * blocSize;long availableSpare = availaBlock * blocSize;return availableSpare;}}



更多相关文章

  1. 安卓巴士自测试题-第二期
  2. Google Map V2在Android中的应用
  3. Android(安卓)BitmapFactory用法总结
  4. android studio 3.0 使用LeakCanary检测内存依赖不上导包导不上
  5. 存储容量检测 注意构建StatFs对象的参数合法性
  6. linux android 下源码编译问题总结(一)
  7. [置顶] android ant 打包全过程 包含library,so,jar,混淆,签名
  8. Android(安卓)-- sqlite数据库随apk发布
  9. 解决gradle下载慢,下载失败的办法

随机推荐

  1. Android实现为GridView添加边框效果
  2. Android(安卓)NDK c调用java代码
  3. Android(安卓)- View Alpha值
  4. Android的Activity获取fragment和fragmen
  5. Android(安卓)全屏与沉浸式
  6. android与j2me移植之clipRect
  7. Android中Dialog的使用
  8. Android使用AudioRecord遇到的问题与解决
  9. Android通知
  10. Android(安卓)MediaRecorder系统结构