Android Question total cellphone storage size

Lucas Eduardo

Active Member
Licensed User
Hello, i am trying to get the total cellphone storage size.

i found this code in JAVA

B4X:
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.getBlockSizeLong();
       long availableBlocks = stat.getAvailableBlocksLong();
       return formatSize(availableBlocks * blockSize);
   }

   public static String getTotalInternalMemorySize() {
       File path = Environment.getDataDirectory();
       StatFs stat = new StatFs(path.getPath());
       long blockSize = stat.getBlockSizeLong();
       long totalBlocks = stat.getBlockCountLong();
       return formatSize(totalBlocks * blockSize);
   }

   public static String getAvailableExternalMemorySize() {
       if (externalMemoryAvailable()) {
           File path = Environment.getExternalStorageDirectory();
           StatFs stat = new StatFs(path.getPath());
           long blockSize = stat.getBlockSizeLong();
           long availableBlocks = stat.getAvailableBlocksLong();
           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.getBlockSizeLong();
           long totalBlocks = stat.getBlockCountLong();
           return formatSize(totalBlocks * blockSize);
       } else {
           return ERROR;
       }
   }

How to convert this code to B4A?

Thank you.
 

Lucas Eduardo

Active Member
Licensed User
i found this code in java https://gist.github.com/toms972/6007571 and i tried to do in b4a

i did based on this too https://developer.android.com/reference/android/os/StatFs

here it's my code

B4X:
Dim getAvailableBytes As Long = nativeMe.RunMethod("getAvailableBytes", Null)
    Dim getTotalBytes As Long = nativeMe.RunMethod("getTotalBytes", Null)
    Dim freeSpace As Long = nativeMe.RunMethod("freeSpace", Null)
    Dim totalSpace As Long = nativeMe.RunMethod("totalSpace", Null)
    Dim busySpace As Long = nativeMe.RunMethod("busySpace", Null)
 
    returnFormatted(getAvailableBytes, "getAvailableBytes: ")
    returnFormatted(getTotalBytes, "getTotalBytes: ")
    returnFormatted(freeSpace, "freeSpace: ")
    returnFormatted(totalSpace, "totalSpace: ")
    returnFormatted(busySpace, "busySpace: ")

B4X:
Sub returnFormatted(number As Long, text As String)
    Dim lenghtStr As String = number
        
    If lenghtStr.Length < 4 Then
            
        Log(text & NumberFormat(number,0,2) & "Bytes")
    Else If lenghtStr.Length > 3 And lenghtStr.Length < 7 Then
        Log(text & NumberFormat((number/1000),0,2) & "KB")
    Else If lenghtStr.Length > 6 And lenghtStr.Length < 10 Then
        Log(text & NumberFormat((number/1000000),0,2) & "MB")
    Else If lenghtStr.Length > 9 Then
        Log(text & NumberFormat((number/1000000000),0,2) & "GB")
    End If
End Sub

JAVA
B4X:
#If JAVA
import android.os.Environment;
import android.os.StatFs;
    private static final long MEGA_BYTE = 1048576;
    /**
     * Calculates total space on disk
     * @param external  If true will query external disk, otherwise will query internal disk.
     * @return Number of mega bytes on disk.
     */
    public static long totalSpace()
    {
        StatFs statFsExternal = getStats(true);
        StatFs statFsInternal = getStats(false);
        long total = ((statFsExternal.getBlockCountLong() * statFsExternal.getBlockSizeLong()) + (statFsInternal.getBlockCountLong() * statFsInternal.getBlockSizeLong()));
        return total;
    }
 
    public static long getTotalBytes()
    {
        StatFs statFsExternal = getStats(true);
        StatFs statFsInternal = getStats(false);
        long availableBytes = statFsExternal.getTotalBytes() + statFsInternal.getTotalBytes();
        return availableBytes;
    }
 
    public static long getAvailableBytes()
    {
        StatFs statFsExternal = getStats(true);
        StatFs statFsInternal = getStats(false);
        long availableBytes = (statFsExternal.getAvailableBytes() + statFsInternal.getAvailableBytes());
        return availableBytes;
    }
    /**
     * Calculates free space on disk
     * @param external  If true will query external disk, otherwise will query internal disk.
     * @return Number of free mega bytes on disk.
     */
    public static long freeSpace()
    {
        StatFs statFsExternal = getStats(true);
        StatFs statFsInternal = getStats(false);
        long availableBlocks = statFsExternal.getAvailableBlocksLong() + statFsInternal.getAvailableBlocksLong();
        long blockSize = statFsExternal.getBlockSizeLong() + statFsInternal.getBlockSizeLong();
        long freeBytes = availableBlocks * blockSize;
        return freeBytes;
    }
    /**
     * Calculates occupied space on disk
     * @param external  If true will query external disk, otherwise will query internal disk.
     * @return Number of occupied mega bytes on disk.
     */
    public static long busySpace()
    {
        StatFs statFsExternal = getStats(true);
        StatFs statFsInternal = getStats(false);
        long total = (statFsExternal.getBlockCountLong() * statFsExternal.getBlockSizeLong()) + (statFsInternal.getBlockCountLong() * statFsInternal.getBlockSizeLong());
        long free  = (statFsExternal.getAvailableBlocksLong() * statFsExternal.getBlockSizeLong()) + (statFsInternal.getAvailableBlocksLong() * statFsInternal.getBlockSizeLong());
        return ((total - free));
    }
    private static StatFs getStats(boolean external){
        String path;
        if (external){
            path = Environment.getExternalStorageDirectory().getAbsolutePath();
        }
        else{
            path = Environment.getRootDirectory().getAbsolutePath();
        }
        return new StatFs(path);
    }
#End IF

here it's the logs
B4X:
getAvailableBytes: 9.37GB
getTotalBytes: 30.2GB
freeSpace: 18.75GB
totalSpace: 30.2GB
busySpace: 20.82GB

the logs are different than my cell phone informations

What could i doing wrong?
 

Attachments

  • Screenshot_20190228-120904_Device maintenance.jpg
    Screenshot_20190228-120904_Device maintenance.jpg
    250.6 KB · Views: 296
Last edited:
Upvote 0
Top