首页 技术 正文
技术 2022年11月20日
0 收藏 590 点赞 4,799 浏览 2049 个字

个人学习,仅供参考!

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;
    }
}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291