This is a simple example that shows how many bytes are free in your SDCard.
Inorder to run this example, you have to create an SDCard and start the emulator with the SDCard.
Now create a fresh project and name it “FreeSpaceActivity.java” and copy the following code to it.
package pack.coderzheaven.check_space; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.os.Environment; import android.os.StatFs; import android.widget.TextView; public class FreeSpaceActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); StatFs stat_fs = new StatFs(Environment.getExternalStorageDirectory().getPath()); double avail_sd_space = (double)stat_fs.getAvailableBlocks() *(double)stat_fs.getBlockSize(); double GB_Available = (avail_sd_space / 1073741824); System.out.println("Available GB : " + GB_Available); TextView tv = (TextView)findViewById(R.id.tv); tv.setText("Your SD Card is " + GB_Available + " bytes free." ); tv.setTextColor(Color.GREEN); tv.setTextSize(20); } }
The main.xml file
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" android:id="@+id/tv" /> </LinearLayout>
AndroidManifest file
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="pack.coderzheaven.check_space" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".FreeSpaceActivity" 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>
Please leave your comments if the post was useful.