How to check SDCard free space in ANDROID?

By | April 19, 2011

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

AndroidManifest file

Please leave your comments if the post was useful.

Free space in SD Card

Free Space in SD Card

Leave a Reply

Your email address will not be published. Required fields are marked *