丽-pc

导航

 

个人学习,仅供参考!

package com.example.administrator.filemanager.utils;


import android.app.ActivityManager;
import android.content.Context;
import android.os.Environment;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;

import static android.os.Environment.getExternalStorageState;


public class MemoryManager {

    /**
     * 获取设备剩余时运行内存
     */
    public static long getPhoneFreeMenory(Context context) {

        //声明内存对象并初始化
        ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();
        //获取服务
        ActivityManager an = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

        //获取剩余时运行内存
        an.getMemoryInfo(info);
        //返回内存大小
        return info.availMem;
    }

    /**
     * 设备全部运行时的内存
     */
    public static long getPhoneTotalMemory() {

        try {
            FileReader fr = new FileReader("/proc/meminfo");
            //输入流
            BufferedReader br = new BufferedReader(fr);
            String text = br.readLine();
            //split:分别字符串
            String[] array = text.split("\\s+");

            // 返回数值  单位:kb
            return Long.valueOf(array[1]) * 1024;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {//IO异常
            e.printStackTrace();
        }

        return 0;
    }

    /*
    * 获取手机内置SD卡
    * */
    public static String getPhoneInSDCardPath() {
       /* File sdState = Environment.getExternalStorageDirectory();*/
       String sdState=getExternalStorageState();
        if (!sdState.equals(Environment.MEDIA_MOUNTED)) {//路径不存在或者为空
            return null;
        }

        return Environment.getExternalStorageDirectory().getAbsolutePath();
    }

    /**
     * 获取手机外置SD卡路径
     */
    public static String getPhoneOutSDCardPath() {
        Map<String, String> map = System.getenv();
        if (map.containsKey("SECONDARY_STORAGE")) {
            String str = map.get("SECONDARY_STORAGE");
            String[] paths = str.split(":");
            //判断路径是否存在
            if (paths == null || paths.length <= 0) {//不存在
                return null;
            }
            return paths[0];
        }
        return null;
    }
}

posted on 2017-01-04 10:29  丽丽博客园  阅读(778)  评论(0编辑  收藏  举报