如何检查android中的内存存储计数?

如何检查android中的内存存储计数?,android,download,progress-bar,custom-controls,Android,Download,Progress Bar,Custom Controls,我想在我的应用程序中获取progressbar中的内存和已用内存,如下图所示。如何应用请帮助我自定义android中使用的progressbar。我使用了下载应用程序 使用android.os.Environment查找内部目录,然后使用android.os.StatFs对其调用Unix StatFs系统调用。来自Android设置应用程序的无耻: File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.

我想在我的应用程序中获取progressbar中的内存和已用内存,如下图所示。如何应用请帮助我自定义android中使用的progressbar。我使用了下载应用程序

使用android.os.Environment查找内部目录,然后使用android.os.StatFs对其调用Unix StatFs系统调用。来自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);

这里是如何实现的:
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;
        }
    }
}
layout/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>

drawable/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上,我得到了以下图片:


由于平台不同,设备上的颜色可能会不同。

谢谢回复!!但是如何在android中设置进度条中的文本。在进度条中使用的内存颜色和可用的内存颜色如何可能请帮助我!!!你试过常用的进度条和两个文本视图吗?是的,我想在进度条中添加两个文本视图。谢谢Sandstar的回复,我会检查的。