zoukankan      html  css  js  c++  java
  • 安卓读取SD卡的容量

    在开发中,我们经常会用到SD卡,那么在对SD卡进行读写的时候,我们经常需要判断SD卡的剩余容量是否足够。因此,这次我们来写写获取SD卡容量的程序。

    该注意的地方,我都在程序里面有注明了。看程序基本就懂了哈。

    先来看看运行结果截图吧。

    布局文件 activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.sdcard.MainActivity" >
    
        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
    
        <TextView
            android:id="@+id/tv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
        
          <TextView
            android:id="@+id/tv3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
    
    </LinearLayout>

    java文件MainActivty.java

    package com.example.sdcard;
    
    import java.io.File;
    
    import android.annotation.SuppressLint;
    import android.app.Activity;
    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.widget.Toast;
    
    @SuppressLint("ShowToast")
    public class MainActivty extends Activity {
    
        @SuppressWarnings("deprecation")
        @SuppressLint("NewApi")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            TextView tv = (TextView) findViewById(R.id.tv);
            TextView tv2 = (TextView) findViewById(R.id.tv2);
            TextView tv3 = (TextView) findViewById(R.id.tv3);
    
            long blockSize;
            long totalBlocks;
            long avaibleBlocks;
            // 判断是否有插入并挂载存储卡
            if (Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {
                File path = Environment.getExternalStorageDirectory();
                StatFs statFs = new StatFs(path.getPath());
                /*
                 * Build.VERSION.SDK_INT:获取当前系统版本的等级
                 * Build.VERSION_CODES.JELLY_BEAN_MR2表示安卓4.3,也就是18,这里直接写18也可以
                 * 因为getBlockSizeLong()等三个方法是安卓4.3以后才有的,所以这里需要判断当前系统版本
                 * 补充一个知识点:所有储存设备都被分成若干块,每一块都有固定大小。
                 */
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                    // 获取块的数量
                    blockSize = statFs.getBlockSizeLong();
                    // 获取一共有多少块
                    totalBlocks = statFs.getBlockCountLong();
                    // 可以活动的块
                    avaibleBlocks = statFs.getAvailableBlocksLong();
                    tv.setText("总空间:" + formatSize(totalBlocks * blockSize));
                    tv2.setText("可用空间:" + formatSize(avaibleBlocks * blockSize));
                    tv3.setText("已用空间:"
                            + formatSize((totalBlocks * blockSize)
                                    - (avaibleBlocks * blockSize)));
    
                } else {
                    /*
                     * 黑线说明这三个API已经过时了。但是为了兼容4.3一下的系统,我们需要写上
                     */
                    blockSize = statFs.getBlockSize();
                    totalBlocks = statFs.getBlockCount();
                    avaibleBlocks = statFs.getAvailableBlocks();
                    tv.setText(formatSize(avaibleBlocks * blockSize));
                }
            } else {
                Toast.makeText(this, "未找到SD卡", 0);
            }
        }
    
        private String formatSize(long size) {
            // 格式化显示的数据。
            return Formatter.formatFileSize(MainActivty.this, size);
        }
    
    }
  • 相关阅读:
    Kubernetes 弹性伸缩全场景解析 (四)- 让核心组件充满弹性
    15分钟在笔记本上搭建 Kubernetes + Istio开发环境
    idea 插件的使用
    jQuery获取select元素选择器练习
    【Maven】添加ueditor到maven本地仓库
    jQuery序列化乱码解决
    Linux安装RedHat
    MyBatis SQL xml处理小于号与大于号
    js判断数据类型
    基于SSM框架的通用权限框架设计
  • 原文地址:https://www.cnblogs.com/linfenghp/p/5392831.html
Copyright © 2011-2022 走看看