Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internal phone storage in Android

Tags:

android

How can you retrieve your phone internal storage from an app? I found MemoryInfo, but it seems that returns information on how much memory use your currently running tasks.

I am trying to get my app to retrieve how much internal phone storage is available.

like image 886
John Avatar asked Apr 16 '10 12:04

John


People also ask

What does internal phone storage mean?

What is internal phone storage? Your internal phone storage (sometimes referred to as 'memory') is the amount of space you have available directly on your phone's drive to store your data. Examples of external storage include microSD cards, memory sticks/USBs, and plug-in hard drives.

How do I use internal storage on Android?

Just open it up to browse any area of your local storage or a connected Drive account; you can either use the file type icons at the top of the screen or, if you want to look folder by folder, tap the three-dot menu icon in the upper-right corner and select "Show internal storage" — then tap the three-line menu icon in ...

Can I delete internal storage Android data?

On an Android phone, go to Settings > StorageIf you drill down into the category, you can delete individual files or data. For example, under “Audio,” long-press on one or more files and tap Delete. Or under “Apps,” tap the app's name and then Uninstall to remove it from your phone.


2 Answers

Use android.os.Environment to find the internal directory, then use android.os.StatFs to call the Unix statfs system call on it. Shamelessly stolen from the Android settings app:

File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return Formatter.formatFileSize(this, availableBlocks * blockSize);
like image 163
Josh Lee Avatar answered Sep 23 '22 13:09

Josh Lee


I had a hard time having mine works. So I would like to share my working code to save some guys some time.
Tested on a 32GB device and 1GB device.

// Return size is in Megabytes
public class DeviceMemory {

        public static long getInternalStorageSpace()
        {
            StatFs statFs = new StatFs(Environment.getDataDirectory().getAbsolutePath());
            //StatFs statFs = new StatFs("/data");
            long total = ((long)statFs.getBlockCount() * (long)statFs.getBlockSize()) / 1048576;
            return total;
        }

        public static long getInternalFreeSpace()
        {
            StatFs statFs = new StatFs(Environment.getDataDirectory().getAbsolutePath());
            //StatFs statFs = new StatFs("/data");
            long free  = ((long)statFs.getAvailableBlocks() * (long)statFs.getBlockSize()) / 1048576;
            return free;
        }

        public static long getInternalUsedSpace()
        {
            StatFs statFs = new StatFs(Environment.getDataDirectory().getAbsolutePath());
            //StatFs statFs = new StatFs("/data");
            long total = ((long)statFs.getBlockCount() * (long)statFs.getBlockSize()) / 1048576;
            long free  = ((long)statFs.getAvailableBlocks() * (long)statFs.getBlockSize()) / 1048576;
            long busy  = total - free;
            return busy;
        }
}
like image 29
Lazy Ninja Avatar answered Sep 22 '22 13:09

Lazy Ninja