Android_02_擷取SD卡的可用容量

來源:互聯網
上載者:User

Android_02_擷取SD卡的可用容量

程式碼範例如下:

 

package com.itheima.getsdavail;import java.io.File;import android.os.Build;import android.os.Bundle;import android.os.Environment;import android.os.StatFs;import android.app.Activity;import android.text.format.Formatter;import android.view.Menu;import android.widget.TextView;public class MainActivity extends Activity {@SuppressWarnings(deprecation)@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);File path = Environment.getExternalStorageDirectory();        StatFs stat = new StatFs(path.getPath());        long blockSize;        long totalBlocks;        long availableBlocks;                //擷取當前系統版本的等級        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
//高版本的Android支援以下操作         blockSize = stat.getBlockSizeLong();             totalBlocks = stat.getBlockCountLong();             availableBlocks = stat.getAvailableBlocksLong();        }
//低版本的Android支援以下操作        else{        blockSize = stat.getBlockSize();            totalBlocks = stat.getBlockCount();            availableBlocks = stat.getAvailableBlocks();        }                TextView tv = (TextView) findViewById(R.id.tv);        tv.setText(formatSize(availableBlocks * blockSize));}private String formatSize(long size) {        return Formatter.formatFileSize(this, size);    }}

 

 

註:可以通過源碼的Setting應用去查看其是如何擷取SD卡的剩餘空間的,從而通過借鑒到達我們擷取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.