閱讀527 返回首頁    go 阿裏雲 go 技術社區[雲棲]


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 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:andro
      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:andro
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
  <Button
  android:
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"  
  android:text="@string/strButton"
  >
  </Button>
  <TextView  
  android:
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  >
  </TextView>
  <ProgressBar 
  android:
  
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:max="100"
  android:progress="0">
  </ProgressBar>
</LinearLayout>






最後更新:2017-04-02 16:48:17

  上一篇:go 關於win7係統 vs2010下的C語言 輸出中文 是亂碼問題
  下一篇:go exampleuse SharedPreferences