android sd卡狀態、路徑、可用空間,記憶體

來源:互聯網
上載者:User

標籤:

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 {    @Override    protected 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的SIZE        long blocSize = statFs.getBlockSize();        // 擷取BLOCK數量        // long totalBlocks = statFs.getBlockCount();        // 可使用的Block的數量        long availaBlock = statFs.getAvailableBlocks();        // long total = totalBlocks * blocSize;        long availableSpare = availaBlock * blocSize;        return availableSpare;    }}


android sd卡狀態、路徑、可用空間,記憶體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.