查看手机存储空间

最近看到了Environment的使用,就做了一个很简单的查看手机空间大小的demo,虽然很简单,但是还是蛮实用的。可以做成下拉状态栏显示,写成服务,开机启动;但是我这里就简单了很多,自己页面点击查看。主要使用到了两个类,一个是Environment,一个就是android.text.format.Formatter;注意导包的时候一定是这个包,不是java.util.Formatter.结果图如下:

 

layout_main.xml文件代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="pay.com.huaya.getsdcardsize.MainActivity">


    <TextView
        android:id="@+id/tv_1"
        android:hint="磁盘总大小"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv_2"
        android:hint="磁盘可用大小"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:onClick="click1"
        android:text="查看磁盘空间"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

<!--存放友情提示信息-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tv_3"
        />
</LinearLayout>

MainActivity.java代码如下:

package pay.com.huaya.getsdcardsize;

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

import java.io.File;


public class MainActivity extends AppCompatActivity {
    private TextView textView1;
    private TextView textView2;
    private TextView textView3;

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


    private void initUI() {
        textView1 = findViewById(R.id.tv_1);
        textView2 = findViewById(R.id.tv_2);
        textView3 = findViewById(R.id.tv_3);

    }

    public void click1(View view) {
        File file = Environment.getExternalStorageDirectory();
        long total = file.getTotalSpace();
        long accese = file.getUsableSpace();
        float percent = (float) accese / total;
        String totalSpace = Formatter.formatFileSize(this, total);
        String acceseSpace = Formatter.formatFileSize(this, accese);
        textView1.setText("总大小:  " + totalSpace);
        textView2.setText("可用大小:" + acceseSpace);

        if (percent > 0.7) {

            textView3.setText("主人,磁盘空间还有很多,没事儿随便用");

        } else if (percent <= 0.7 && percent >= 0.4) {
            textView3.setText("主人,磁盘空间目前够用,不过以后要合理使用了");
        } else {
            textView3.setText("主人,磁盘空间不多了,要节约使用啦");
        }
    }

}

这里主要注意一下Environment这个类和Formtter这个类的使用,我们获取一些文件路径都是通过前者,不是写死的;后面那个类可以不使用,我们进入源码可以看出,Formtter调用的formatFileSize()方法底层调用的是formatBytes(Resources res, long sizeBytes, int flags)方法,其源码如下:

public static BytesResult formatBytes(Resources res, long sizeBytes, int flags) {
    final boolean isNegative = (sizeBytes < 0);
    float result = isNegative ? -sizeBytes : sizeBytes;
    int suffix = com.android.internal.R.string.byteShort;
    long mult = 1;
    if (result > 900) {
        suffix = com.android.internal.R.string.kilobyteShort;
        mult = 1000;
        result = result / 1000;
    }
    if (result > 900) {
        suffix = com.android.internal.R.string.megabyteShort;
        mult *= 1000;
        result = result / 1000;
    }
    if (result > 900) {
        suffix = com.android.internal.R.string.gigabyteShort;
        mult *= 1000;
        result = result / 1000;
    }
    if (result > 900) {
        suffix = com.android.internal.R.string.terabyteShort;
        mult *= 1000;
        result = result / 1000;
    }
    if (result > 900) {
        suffix = com.android.internal.R.string.petabyteShort;
        mult *= 1000;
        result = result / 1000;
    }
    // Note we calculate the rounded long by ourselves, but still let String.format()
    // compute the rounded value. String.format("%f", 0.1) might not return "0.1" due to
    // floating point errors.
    final int roundFactor;
    final String roundFormat;
    if (mult == 1 || result >= 100) {
        roundFactor = 1;
        roundFormat = "%.0f";
    } else if (result < 1) {
        roundFactor = 100;
        roundFormat = "%.2f";
    } else if (result < 10) {
        if ((flags & FLAG_SHORTER) != 0) {
            roundFactor = 10;
            roundFormat = "%.1f";
        } else {
            roundFactor = 100;
            roundFormat = "%.2f";
        }
    } else { // 10 <= result < 100
        if ((flags & FLAG_SHORTER) != 0) {
            roundFactor = 1;
            roundFormat = "%.0f";
        } else {
            roundFactor = 100;
            roundFormat = "%.2f";
        }
    }

    if (isNegative) {
        result = -result;
    }
    final String roundedString = String.format(roundFormat, result);

    // Note this might overflow if abs(result) >= Long.MAX_VALUE / 100, but that's like 80PB so
    // it's okay (for now)...
    final long roundedBytes =
            (flags & FLAG_CALCULATE_ROUNDED) == 0 ? 0
            : (((long) Math.round(result * roundFactor)) * mult / roundFactor);

    final String units = res.getString(suffix);

    return new BytesResult(roundedString, units, roundedBytes);
}

可见我们得到的long型数值为字节数,我们自己也可以转换成M单位。这里就不细说了

注:欢迎扫码关注

 

展开阅读全文

没有更多推荐了,返回首页