私は内部ストレージにメモリを取得したいのですが、私のアプリの以下の画像のようにプログレスバーのメモリと使用済みメモリを取得します。適用方法は、Androidで使用されるプログレスバーをカスタマイズするのを手伝ってください。ダウンロードアプリを使用しました。
5111 次
2 に答える
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;
}
}
}
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.1androidを搭載したxperiavで次の画像が表示されます。
プラットフォームが異なるため、デバイスの色が異なる場合があります。
于 2013-03-20T09:15:08.677 に答える
0
android.os.Environmentを使用して内部ディレクトリを検索し、次にandroid.os.StatFsを使用してUnixstatfsシステムコールを呼び出します。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);
この答えから
于 2013-03-20T07:44:53.397 に答える