Android_(自动化)获取手机存储卡的容量

手机上的存储卡是可以随时插拔的,每次插拔时会像操作系统总发送Action广播事件。

使用StatFs文件系统来获取MicroSD存储卡的剩余容量,在使用前先判断是否插入了存储卡,如果不存在则不于计算

运行截图:

程序结构

package com.example.asus.gary_024;

import android.os.Environment;
import android.os.StatFs;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.io.File;
import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity {
        private Button myButton;
        private ProgressBar myProgressBar;
        private TextView myTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myButton = (Button)findViewById(R.id.mybutton);
        myProgressBar = (ProgressBar)findViewById(R.id.myprogressBar);
        myTextView = (TextView)findViewById(R.id.mytextView);

        myButton.setOnClickListener(new Button.OnClickListener()
        {

            @Override
            public void onClick(View v) {
                showSize();
            }
        });
    }

    private void showSize(){
        //将TextView及ProgressBar设置空及为0
        myTextView.setText("");
        myProgressBar.setProgress(0);

        //判断储存卡是否插入
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
        {
            //将取得SD CARD文件路径一般是/sdcard
            File path = Environment.getExternalStorageDirectory();
            //通过StatFs看文件系统空间使用状况
            StatFs statFs = new StatFs(path.getPath());
            //Block数量
            long blockSize = statFs.getBlockSize();
            //总Block数量
            long totalBlocks = statFs.getBlockCount();
            //已使用的Block数量
            long availableBlocks = statFs.getAvailableBlocks();
            String[] total = fileSize(totalBlocks*blockSize);
            String[] available=fileSize(availableBlocks*blockSize);
            //使用getMax取得在main.xml里设置的ProgressBar最大值
            int ss = Integer.parseInt(available[0])*myProgressBar.getMax()/Integer.parseInt(total[0]);

            myProgressBar.setProgress(ss);
            String text = "总共"+total[0]+total[1]+"\n";
            text+="可用"+available[0]+available[1];
            myTextView.setText(text);
       }else if(Environment.getExternalStorageState().equals(
               Environment.MEDIA_REMOVED)){
            String text = "SD CARD已删除";
            myTextView.setText(text);
        }
    }
    //返回为字符串数组[0]的大小为[1]\单位为KB或MB
    private String[] fileSize(long size)
    {
        String str = "";
        size /=1024;
        if(size>=1024)
        {
            str = "KB";
            size/=1024;
            if(size>=1024)
            {
                str = "MB";
                size/=1024;
            }
        }
        DecimalFormat formatter = new DecimalFormat();
        //每3个数字分隔,如1,000
        formatter.setGroupingSize(3);
        String result[] = new String[2];
        result[0] = formatter.format(size);
        result[1]=str;
        return result;
    }

}
MainActivity
<?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="com.example.asus.gary_024.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Gary_获得手机存储卡的容量"
        android:textSize="40px" />

    <Button
        android:id="@+id/mybutton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="存储卡容量" />

    <TextView
        android:id="@+id/mytextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="40px" />

    <ProgressBar
        android:id="@+id/myprogressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:max="100"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
activity_main.xml

一、界面布局

  一个Button按钮,一个TextView文本框和一个ProgressBar Widget控件

  点击Button按钮,在TextView文本框上显示手机卡的容量和手机卡剩余容量的大小,在ProgressBar Widget控件中能更好的显示容量,使显示效果一目了然

 二、实现程序功能

1、添加Button按钮的单机事件响应机制,当点击Button按钮监听事件setOnClickListener时,调用showSize()显示存储卡的剩余容量

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myButton = (Button)findViewById(R.id.mybutton);
        myProgressBar = (ProgressBar)findViewById(R.id.myprogressBar);
        myTextView = (TextView)findViewById(R.id.mytextView);

        myButton.setOnClickListener(new Button.OnClickListener()
        {

            @Override
            public void onClick(View v) {
                showSize();
            }
        });
    }

2、定义showSize()方法来显示存储卡的容量大小,具体实现流程如下

  分别设置TextView和ProgressBarw为空值

  获取SD CARD文件路径

  通过StatFs来查看文件系统控件使用情况

  分别获取总的Block数量和已使用的Block数量

  通过getMax获取在activity_main.xml中设置ProgressBar的最大值

  显示容量信息

  如果没有SD卡则输出"SD CARD已删除"提示

  private void showSize(){
        //将TextView及ProgressBar设置空及为0
        myTextView.setText("");
        myProgressBar.setProgress(0);

        //判断储存卡是否插入
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
        {
            //将取得SD CARD文件路径一般是/sdcard
            File path = Environment.getExternalStorageDirectory();
            //通过StatFs看文件系统空间使用状况
            StatFs statFs = new StatFs(path.getPath());
            //Block数量
            long blockSize = statFs.getBlockSize();
            //总Block数量
            long totalBlocks = statFs.getBlockCount();
            //已使用的Block数量
            long availableBlocks = statFs.getAvailableBlocks();
            String[] total = fileSize(totalBlocks*blockSize);
            String[] available=fileSize(availableBlocks*blockSize);
            //使用getMax取得在main.xml里设置的ProgressBar最大值
            int ss = Integer.parseInt(available[0])*myProgressBar.getMax()/Integer.parseInt(total[0]);

            myProgressBar.setProgress(ss);
            String text = "总共"+total[0]+total[1]+"\n";
            text+="可用"+available[0]+available[1];
            myTextView.setText(text);
       }else if(Environment.getExternalStorageState().equals(
               Environment.MEDIA_REMOVED)){
            String text = "SD CARD已删除";
            myTextView.setText(text);
        }

3、在获取文件大小默认是字节,可以调用fileSize()来进行进制大小转换

  //返回为字符串数组[0]的大小为[1]\单位为KB或MB
    private String[] fileSize(long size)
    {
        String str = "";
        size /=1024;
        if(size>=1024)
        {
            str = "KB";
            size/=1024;
            if(size>=1024)
            {
                str = "MB";
                size/=1024;
            }
        }
        DecimalFormat formatter = new DecimalFormat();
        //每3个数字分隔,如1,000
        formatter.setGroupingSize(3);
        String result[] = new String[2];
        result[0] = formatter.format(size);
        result[1]=str;
        return result;
    }

                                         

备注:

           在使用Android模拟器时,可以使用FAT32格式的磁盘镜像作为SD卡的模拟,具体实现过程如下:

Step1: 进入Android SDK目录文件下的tools子目录,运行如下命令

 
mksdcard -1 sdcard 512M/your_path_for_img/sdcard.img

入这样就可以创建了一个512MB的SD卡镜像文件
 
Step2: 在运行模拟器狮指定模拟存储卡路径,注意需要使用完整路径
 
emulator -sdcard/your_path_for_img/sdcard.img
 
在此时模拟器中就可以使用"/sdcard"这个路径来指向模拟的SD卡了。
 
在使用mksdcard命令时需要注意如下6点。
 
1)mycard命令可以使用3种尺寸:字节、KB和MB。如果只使用数字,表示字节。后面跟K,如262144K,表示262144KB,也可写为256M
 
2)mycard建立的虚拟文件最小为8MB,也就是说,模拟器只支持大于8MB的虚拟文件
 
3)-l命令行参数表示虚拟磁盘的卷标,可以没有该参数
 
4)虚拟文件的拓展名可以是任意的,如mycard.abc
 
5)mksdcard命令不会自动创建不存在的目录,因此,在执行上面的命令之前,要先在当前目录种创建一个card目录
 
6)mksdcard命令就是按实际大小生成的sdcard虚拟文件。也就是说,生成256MB的虚拟文件的尺寸就是256MB,如果要生成较大的虚拟文件,要看看自己的硬盘控件是否足够
 
执行完上面的命令后,执行下面的命令可以启动Android模拟器。
 
emulator -avd avd1 -sdcard card/mycard img

猜你喜欢

转载自www.cnblogs.com/1138720556Gary/p/9108808.html