Android裝置記憶體和SD卡操作工具類

來源:互聯網
上載者:User

package cc.c;import java.io.File;import java.util.List;import android.os.StatFs;import java.io.FileReader;import java.io.IOException;import java.io.BufferedReader;import android.os.Environment;import android.content.Context;import android.app.ActivityManager;import android.app.ActivityManager.MemoryInfo;import android.app.ActivityManager.RunningAppProcessInfo;public class StorageUtil {private static final int ERROR = -1;public static int save_dir = 1;//判斷是否已經安裝SD卡public static boolean isSDCardExist() {return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);}//記憶體清理//注意許可權://public static void cleanMemory(Context context){System.out.println("---> 清理前可用記憶體:"+getAvailMemory(context)+"MB");ActivityManager activityManager=(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);RunningAppProcessInfo runningAppProcessInfo=null;List  runningAppProcessInfoList= activityManager.getRunningAppProcesses();//List serviceInfos = mActivityManager.getRunningServices(100);if (runningAppProcessInfoList != null) {for (int i = 0; i < runningAppProcessInfoList.size(); ++i) {runningAppProcessInfo= runningAppProcessInfoList.get(i);// 一般數值大於RunningAppProcessInfo.IMPORTANCE_SERVICE// 的進程即為長時間未使用進程或者空進程// 一般數值大於RunningAppProcessInfo.IMPORTANCE_VISIBLE// 的進程都是非可見進程,即在後台運行if (runningAppProcessInfo.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) {String[] pkgList = runningAppProcessInfo.pkgList;for (int j = 0; j < pkgList.length; ++j) {System.out.println("===> 正在清理:"+pkgList[j]);activityManager.killBackgroundProcesses(pkgList[j]);}}}}System.out.println("---> 清理後可用記憶體:"+getAvailMemory(context)+"MB");}//擷取記憶體總大小public static long getTotalMemory() {//系統的記憶體資訊檔String filePath = "/proc/meminfo";String lineString;String[] stringArray;long totalMemory = 0;try {FileReader fileReader = new FileReader(filePath);BufferedReader bufferedReader = new BufferedReader(fileReader,1024 * 8);//讀取meminfo第一行,擷取系統總記憶體大小lineString = bufferedReader.readLine();//按照空格拆分stringArray = lineString.split("\\s+");//獲得系統總記憶體,單位KBtotalMemory = Integer.valueOf(stringArray[1]).intValue();bufferedReader.close();} catch (IOException e) {}return totalMemory / 1024;}//擷取可用記憶體大小public static long getAvailMemory(Context context) {ActivityManager activityManager=(ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);MemoryInfo memoryInfo = new MemoryInfo();activityManager.getMemoryInfo(memoryInfo);return memoryInfo.availMem / (1024 * 1024);}//SD卡剩餘空間public static long getAvailableExternalMemorySize() {if (isSDCardExist()) {File path = Environment.getExternalStorageDirectory();StatFs stat = new StatFs(path.getPath());long blockSize = stat.getBlockSize();long availableBlocks = stat.getAvailableBlocks();return availableBlocks * blockSize;} else {return ERROR;}}//SD卡總空間public static long getTotalExternalMemorySize() {if (isSDCardExist()) {File path = Environment.getExternalStorageDirectory();StatFs stat = new StatFs(path.getPath());long blockSize = stat.getBlockSize();long totalBlocks = stat.getBlockCount();return totalBlocks * blockSize;} else {return ERROR;}}//判斷SD卡下external_sd檔案夾的總大小public static long getTotalExternal_SDMemorySize() {if (isSDCardExist()) {File path = Environment.getExternalStorageDirectory();File externalSD = new File(path.getPath() + "/external_sd");if (externalSD.exists() && externalSD.isDirectory()) {StatFs stat = new StatFs(path.getPath() + "/external_sd");long blockSize = stat.getBlockSize();long totalBlocks = stat.getBlockCount();if (getTotalExternalMemorySize() != -1&& getTotalExternalMemorySize() != totalBlocks* blockSize) {return totalBlocks * blockSize;} else {return ERROR;}} else {return ERROR;}} else {return ERROR;}}//判斷SD卡下external_sd檔案夾的可用大小public static long getAvailableExternal_SDMemorySize() {if (isSDCardExist()) {File path = Environment.getExternalStorageDirectory();File externalSD = new File(path.getPath() + "/external_sd");if (externalSD.exists() && externalSD.isDirectory()) {StatFs stat = new StatFs(path.getPath() + "/external_sd");long blockSize = stat.getBlockSize();long availableBlocks = stat.getAvailableBlocks();if (getAvailableExternalMemorySize() != -1&& getAvailableExternalMemorySize() != availableBlocks* blockSize) {return availableBlocks * blockSize;} else {return ERROR;}} else {return ERROR;}} else {return ERROR;}}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.