android 存储操作 大小显示换算 kb mb KB MB 读取

简介: package irdc.cunchu;import irdc.cunchu.R;import java.io.File;import java.text.DecimalFormat;import android.app.Activity;import android.os.Bundle;import android.os.Environment;import and

package irdc.cunchu;

import irdc.cunchu.R;

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

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class cunchu extends Activity
{
  private Button myButton;
  private ProgressBar myProgressBar;
  private TextView myTextView;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.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 arg0)
      {
        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的size*/
      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 = "";
    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;
  }

}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="irdc.cunchu"
      android:versionCode="1"
      android:versionName="1.0.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name="irdc.cunchu.cunchu"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest> 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
  <Button
  android:id="@+id/myButton"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"  
  android:text="@string/strButton"
  >
  </Button>
  <TextView  
  android:id="@+id/myTextView"
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  >
  </TextView>
  <ProgressBar 
  android:id="@+id/myProgressBar"
  style="?android:attr/progressBarStyleHorizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:max="100"
  android:progress="0">
  </ProgressBar>
</LinearLayout>






目录
相关文章
|
7月前
|
Java Android开发
Android oom pthread_create (1040KB stack)分析及解决
Android oom pthread_create (1040KB stack)分析及解决
87 0
|
3天前
|
存储 缓存 安全
Android系统 应用存储路径与权限
Android系统 应用存储路径与权限
6 0
Android系统 应用存储路径与权限
|
1月前
|
存储 Android开发 C++
【Android 从入门到出门】第五章:使用DataStore存储数据和测试
【Android 从入门到出门】第五章:使用DataStore存储数据和测试
36 3
|
6月前
|
存储 安全 Java
Android DataStore:安全存储和轻松管理数据
Android DataStore:安全存储和轻松管理数据
|
8月前
|
数据库 Android开发 数据库管理
Android使用Room操作SQLite数据库让其变得无比高效和简洁(进一步完善用RecyclerView显示数据库中的数据)
Android使用Room操作SQLite数据库让其变得无比高效和简洁(进一步完善用RecyclerView显示数据库中的数据)
52 0
|
4月前
|
Android开发
[Android]视图的控触操作-MotionEvent
[Android]视图的控触操作-MotionEvent
31 0
|
4月前
|
算法 Java 数据安全/隐私保护
Android App开发之利用JNI实现加密和解密操作实战(附源码 简单易懂)
Android App开发之利用JNI实现加密和解密操作实战(附源码 简单易懂)
76 0
|
8月前
|
数据库 Android开发 数据库管理
Android 使用Room操作数据库进行数据库版本的升级和迁移
Android 使用Room操作数据库进行数据库版本的升级和迁移
427 0
|
8月前
|
存储 数据库 Android开发
Android 使用Room操作SQLite数据库让其变得无比高效和简洁(前一篇文章的完善)
Android 使用Room操作SQLite数据库让其变得无比高效和简洁(前一篇文章的完善)
117 0
|
8月前
|
数据库 Android开发 数据库管理
Android 使用Room操作SQLite数据库让其变得无比高效和简洁(教程一)
Android 使用Room操作SQLite数据库让其变得无比高效和简洁(教程一)
140 0