Android 擷取儲存空間

來源:互聯網
上載者:User

標籤:

package com.example.getMem;

import java.io.File;

import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter;
import android.widget.TextView;
import android.app.Activity;
import android.app.AlertDialog.Builder;


public class MainActivity extends Activity {

    private TextView tv_totalSize;
    private TextView tv_availabelSize;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        tv_totalSize=(TextView) findViewById(R.id.totalSize);
        tv_availabelSize=(TextView) findViewById(R.id.availabel);
        
        // 擷取儲存空間
        File dataFileDir=Environment.getDataDirectory();
        String data =getMemoryInfo(dataFileDir);
        String []d= data.split("##");
        
        tv_totalSize.setText("內部儲存總大小:" + d[0]);
        tv_availabelSize.setText("內部儲存可用空間是:" + d[1]);
        
        //擷取Sd 卡空間
        
//        File SdFileDir=Environment.getExternalStorageDirectory();
//        //判斷Sd卡是否可用
//        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//            String sdMemory =getMemoryInfo(SdFileDir);
//            String []sd=sdMemory.split("##");
//            tv_totalSize.setText("內部儲存總大小:" + sd[0]);
//            tv_availabelSize.setText("內部儲存可用空間是:" + sd[1]);
//            
//        }
        
    }
    private String getMemoryInfo(File path) {
        long blockSize;
        long totalBlockCount;
        long avaiLabelCount;
        // TODO Auto-generated method stub
        StatFs stat = new StatFs(path.getPath());
        
        //檢測系統版本
        if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.JELLY_BEAN_MR2){
        
        //擷取每個扇區的大小
         blockSize = stat.getBlockSizeLong();        
        
        //擷取總共有多少扇區
         totalBlockCount=stat.getBlockCountLong();
        
        //擷取可用扇區數量
         avaiLabelCount=stat.getAvailableBlocksLong();
         }else{
             blockSize=stat.getBlockSize();
             totalBlockCount=stat.getBlockCount();
             avaiLabelCount=stat.getAvailableBlocks();
            
         }
        
        

        
        // 磁碟總大小
        String totalMemory =Formatter.formatFileSize(this, blockSize*totalBlockCount);
        // 可用大小
        String availabelMemory=Formatter.formatFileSize(this, blockSize*avaiLabelCount);
        
        return totalMemory+"##"+availabelMemory;
    }


    
    

}


http://pan.baidu.com/s/1bp0V87d

Android 擷取儲存空間

聯繫我們

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