Android核心技术-day02-03-获取手机内部存储空间

package com.gaozewen.storagespace;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.text.format.Formatter;
import android.widget.TextView;

import java.io.File;

public class MainActivity extends AppCompatActivity {

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

        // 获取我们的存储空间
        File dataFile = Environment.getDataDirectory(); // 手机内部存储空间
        File sdFile = Environment.getExternalStorageDirectory(); // sd 卡存储空间

        long dataSize = dataFile.getTotalSpace();
        long sdSize = sdFile.getTotalSpace();

        TextView tv = findViewById(R.id.tv);
        // 注意:各个手机内部存储和外部存储获取到的都不相同
        tv.setText(String.format("内部存储空间:%s\n外部存储:%s", Formatter.formatFileSize(this, dataSize), Formatter.formatFileSize(this, sdSize)));

    }
}

 

你可能感兴趣的:(Android)