- activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/GET_MEMORY_DETAIL"
android:textColor="@color/Black"
android:textSize="20sp" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@color/Gray_divider" />
<TextView
android:id="@+id/txtAvailableInternal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textColor="@color/Gray_Text" />
<TextView
android:id="@+id/txtTotalInternal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/Red" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:background="@color/Gray_divider" />
<TextView
android:id="@+id/txtExternalAvailable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/Green" />
<TextView
android:id="@+id/txtAvailableExternal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/Blue" />
<TextView
android:id="@+id/txtTotalExternal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/Orange_divider" />
</LinearLayout>
- values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="Green">#006400</color>
<color name="Blue">#0e72c8</color>
<color name="Red">#CC0000</color>
<color name="Black">#000000</color>
<color name="Gray_divider">#ccc</color>
<color name="list_divider">#F2F2F2</color>
<color name="Label_bg">#ececec</color>
<color name="Orange_divider">#FE9A2E</color>
<color name="Gray_Text">#808080</color>
<color name="Gray_Header">#EBEBEB</color>
</resources>
- values/string.xml
<string name="GET_MEMORY_DETAIL">GET MEMORY DETAIL</string>
- MainActivity.java
public class MainActivity extends Activity {
private TextView txtAvailableInternal, txtTotalInternal,
txtExternalAvailable, txtAvailableExternal, txtTotalExternal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtAvailableInternal = (TextView) findViewById(R.id.txtAvailableInternal);
txtTotalInternal = (TextView) findViewById(R.id.txtTotalInternal);
txtExternalAvailable = (TextView) findViewById(R.id.txtExternalAvailable);
txtAvailableExternal = (TextView) findViewById(R.id.txtAvailableExternal);
txtTotalExternal = (TextView) findViewById(R.id.txtTotalExternal);
txtAvailableInternal.setText("Available Internal MemorySize : "
+ getAvailableInternalMemorySize());
txtTotalInternal.setText("Total Internal MemorySize : "
+ getTotalInternalMemorySize());
txtExternalAvailable.setText("External Memory Available ? "
+ String.valueOf(externalMemoryAvailable()));
txtAvailableExternal.setText("Available External MemorySize : "
+ getAvailableExternalMemorySize());
txtTotalExternal.setText("Total External MemorySize : "
+ getTotalExternalMemorySize());
}
public static boolean externalMemoryAvailable() {
return android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
}
public static String getAvailableInternalMemorySize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return formatSize(availableBlocks * blockSize);
}
public static String getTotalInternalMemorySize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return formatSize(totalBlocks * blockSize);
}
public static String getAvailableExternalMemorySize() {
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return formatSize(availableBlocks * blockSize);
} else {
return "ERROR";
}
}
public static String getTotalExternalMemorySize() {
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return formatSize(totalBlocks * blockSize);
} else {
return "ERROR";
}
}
public static String formatSize(long size) {
String suffix = null;
if (size >= 1024) {
suffix = "KB";
size /= 1024;
if (size >= 1024) {
suffix = "MB";
size /= 1024;
}
}
StringBuilder resultBuffer = new StringBuilder(Long.toString(size));
int commaOffset = resultBuffer.length() - 3;
while (commaOffset > 0) {
resultBuffer.insert(commaOffset, ',');
commaOffset -= 3;
}
if (suffix != null)
resultBuffer.append(suffix);
return resultBuffer.toString();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
No comments:
Post a Comment