第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何檢查android設備上的可用空間?在SD卡上?

如何檢查android設備上的可用空間?在SD卡上?

函數式編程 2019-12-25 11:22:17
如何查看Android設備上還剩下多少MB或GB?我正在使用JAVA和android SDK 2.0.1。是否有任何系統服務會公開這樣的信息?
查看完整描述

3 回答

?
holdtom

TA貢獻1805條經驗 獲得超10個贊

試試這個代碼:


StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());


long bytesAvailable = (long)stat.getBlockSize() *(long)stat.getBlockCount();

long megAvailable   = bytesAvailable / 1048576;


System.out.println("Megs :"+megAvailable);

更新:

getBlockCount() -SD卡的返回大小;


getAvailableBlocks() -返回普通程序仍可訪問的塊數(感謝喬)


查看完整回答
反對 回復 2019-12-25
?
桃花長相依

TA貢獻1860條經驗 獲得超8個贊

Yaroslav的答案將給出SD卡的大小,而不是可用空間。StatFs getAvailableBlocks()將返回普通程序仍可訪問的塊數。這是我正在使用的功能:


public static float megabytesAvailable(File f) {

    StatFs stat = new StatFs(f.getPath());

    long bytesAvailable = (long)stat.getBlockSize() * (long)stat.getAvailableBlocks();

    return bytesAvailable / (1024.f * 1024.f);

}

上面的代碼引用了一些不推薦使用的功能。下面我復制一個更新的版本:

public static float megabytesAvailable(File f) {

    StatFs stat = new StatFs(f.getPath());

    long bytesAvailable = 0;

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2)

        bytesAvailable = (long) stat.getBlockSizeLong() * (long) stat.getAvailableBlocksLong();

    else

        bytesAvailable = (long) stat.getBlockSize() * (long) stat.getAvailableBlocks();

    return bytesAvailable / (1024.f * 1024.f);

}


查看完整回答
反對 回復 2019-12-25
?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊

我設計了一些現成的函數來獲取不同單位的可用空間。您可以通過簡單地將其中任何一種復制到項目中來使用這些方法。


/**

 * @return Number of bytes available on External storage

 */

public static long getAvailableSpaceInBytes() {

    long availableSpace = -1L;

    StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());

    availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();


    return availableSpace;

}



/**

 * @return Number of kilo bytes available on External storage

 */

public static long getAvailableSpaceInKB(){

    final long SIZE_KB = 1024L;

    long availableSpace = -1L;

    StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());

    availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();

    return availableSpace/SIZE_KB;

}

/**

 * @return Number of Mega bytes available on External storage

 */

public static long getAvailableSpaceInMB(){

    final long SIZE_KB = 1024L;

    final long SIZE_MB = SIZE_KB * SIZE_KB;

    long availableSpace = -1L;

    StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());

    availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();

    return availableSpace/SIZE_MB;

}


/**

 * @return Number of gega bytes available on External storage

 */

public static long getAvailableSpaceInGB(){

    final long SIZE_KB = 1024L;

    final long SIZE_GB = SIZE_KB * SIZE_KB * SIZE_KB;

    long availableSpace = -1L;

    StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());

    availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();

    return availableSpace/SIZE_GB;

}


查看完整回答
反對 回復 2019-12-25
  • 3 回答
  • 0 關注
  • 596 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號