2013-03-20 43 views
0

我想要Iternal存儲獲取內存和使用的內存在進度欄中像在我的app.how應用程序下面的圖像請幫助我在android.and中使用進度條我使用下載的應用程序。 enter image description here如何在android中檢查內部存儲器數量?

+0

你有沒有嘗試通常的進度欄和兩個文本視圖對齊到不同的邊? – sandrstar 2013-03-20 07:58:44

+0

是的,我想要2個textview裏面的進度條。 – crickpatel0024 2013-03-20 08:06:19

回答

2

下面是它如何來完成:
TestActivity.java:

public class TestActivity extends Activity { 

    /** 
    * Called when the activity is first created. 
    */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     final TextView occupiedSpaceText = (TextView)findViewById(R.id.occupiedSpace); 
     final TextView freeSpaceText = (TextView)findViewById(R.id.freeSpace); 
     final ProgressBar progressIndicator = (ProgressBar)findViewById(R.id.indicator); 
     final float totalSpace = DeviceMemory.getInternalStorageSpace(); 
     final float occupiedSpace = DeviceMemory.getInternalUsedSpace(); 
     final float freeSpace = DeviceMemory.getInternalFreeSpace(); 
     final DecimalFormat outputFormat = new DecimalFormat("#.##"); 

     if (null != occupiedSpaceText) { 
      occupiedSpaceText.setText(outputFormat.format(occupiedSpace) + " MB"); 
     } 

     if (null != freeSpaceText) { 
      freeSpaceText.setText(outputFormat.format(freeSpace) + " MB"); 
     } 

     if (null != progressIndicator) { 
      progressIndicator.setMax((int) totalSpace); 
      progressIndicator.setProgress((int)occupiedSpace); 
     } 
    } 

    /** 
    * From question http://stackoverflow.com/questions/2652935/android-internal-phone-storage by Lazy Ninja 
    */ 
    public static class DeviceMemory { 

     public static float getInternalStorageSpace() { 
      StatFs statFs = new StatFs(Environment.getDataDirectory().getAbsolutePath()); 
      //StatFs statFs = new StatFs("/data"); 
      float total = ((float)statFs.getBlockCount() * statFs.getBlockSize())/1048576; 
      return total; 
     } 

     public static float getInternalFreeSpace() { 
      StatFs statFs = new StatFs(Environment.getDataDirectory().getAbsolutePath()); 
      //StatFs statFs = new StatFs("/data"); 
      float free = ((float)statFs.getAvailableBlocks() * statFs.getBlockSize())/1048576; 
      return free; 
     } 

     public static float getInternalUsedSpace() { 
      StatFs statFs = new StatFs(Environment.getDataDirectory().getAbsolutePath()); 
      //StatFs statFs = new StatFs("/data"); 
      float total = ((float)statFs.getBlockCount() * statFs.getBlockSize())/1048576; 
      float free = ((float)statFs.getAvailableBlocks() * statFs.getBlockSize())/1048576; 
      float busy = total - free; 
      return busy; 
     } 
    } 
} 

佈局/ main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 


    <ProgressBar 
     android:id="@+id/indicator" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     style="@android:style/Widget.ProgressBar.Horizontal" 
     android:progressDrawable="@drawable/memory_indicator_progress" /> 

    <TextView 
     android:id="@+id/occupiedSpace" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentBottom="true" 
     android:gravity="left" 
     android:layout_marginLeft="5dp" 
     android:textColor="@android:color/black" 
     android:textStyle="bold" /> 
    <TextView 
      android:id="@+id/freeSpace" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentBottom="true" 
      android:gravity="right" 
      android:layout_marginRight="5dp" 
      android:textColor="@android:color/black" 
      android:textStyle="bold" /> 
</RelativeLayout> 

繪製/ memory_indicator_progress.xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:id="@android:id/background"> 
     <shape android:shape="rectangle"> 
      <solid android:color="@android:color/holo_green_light" /> 
     </shape> 
    </item> 
    <item android:id="@android:id/progress"> 
     <clip> 
      <shape android:shape="rectangle"> 
       <solid android:color="@android:color/darker_gray" /> 
      </shape> 
     </clip> 
    </item> 

</layer-list> 

我不知道這是否正是你要找的,但我與4.1的Android手機XPERIA V I得到如下圖: enter image description here

在您由於平臺不同,設備顏色可能會有所不同。

+0

感謝Sandrstar的回覆我會檢查它。 – crickpatel0024 2013-03-20 09:30:27

0

使用android.os.Environment查找內部目錄,然後使用android.os.StatFs調用它的Unix statfs系統調用。無恥地stolen從Android應用程序的設置:

File path = Environment.getDataDirectory(); 
StatFs stat = new StatFs(path.getPath()); 
long blockSize = stat.getBlockSize(); 
long availableBlocks = stat.getAvailableBlocks(); 
return Formatter.formatFileSize(this, availableBlocks * blockSize); 

this answer

+0

感謝您的回覆!!但如何在android.used內存顏色和可用內存顏色進度條中設置文本進度條如何可能請幫助我! – crickpatel0024 2013-03-20 07:48:30

相關問題