12 获取外部存储卡大小

package com.fmy.state;

import java.text.Format;

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

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void lick(View v) {

        StatFs state = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());
        long free;
        //大于18的已经启用getFreeBlocks和 getBlockSize
        //Build.VERSION.SDK_INT>=18获取当前运行时在那个版本
        if (Build.VERSION.SDK_INT>=18){
            free = state.getFreeBytes();
        }else{
            free = state.getFreeBlocks()*state.getBlockSize();
        }
        //格式化内存大小 如果自动转化为KB或者M, G
        String formatFileSize = Formatter.formatFileSize(this, free);

        Toast.makeText(this, formatFileSize, 0).show();
    }
}

posted on 2016-09-06 20:38  木鱼哥  阅读(106)  评论(0编辑  收藏  举报

导航