Android 如何获取外部存储sd卡大小(已安装sd卡)?

Android 如何获取外部存储sd卡大小(已安装sd卡)?,android,storage,sd-card,android-external-storage,Android,Storage,Sd Card,Android External Storage,我添加这行是为了查找大小(内部和外部)大小 我在我的平板电脑上测试。我得到的是内部和外部SD卡的大小 在内部存储器中: 总内存——1007 可用内存--683 在外部存储器中: 总内存--1763 可用内存--1554 但在平板电脑中,我看到了设置。外部存储大小为8GB。但是,当我通过编程方式进行测试时,它向我展示了1.7GB 查找外部存储大小的步骤是什么?要获取外部SD卡的可用“空闲”空间,以显示符合菜单->设置->SD卡和电话存储号码的号码,请使用以下代码: StatFs stat = ne

我添加这行是为了查找大小(内部和外部)大小

我在我的平板电脑上测试。我得到的是内部和外部SD卡的大小

在内部存储器中:

  • 总内存——1007
  • 可用内存--683
  • 在外部存储器中:

  • 总内存--1763
  • 可用内存--1554
  • 但在平板电脑中,我看到了设置。外部存储大小为8GB。但是,当我通过编程方式进行测试时,它向我展示了1.7GB


    查找外部存储大小的步骤是什么?

    要获取外部SD卡的可用“空闲”空间,以显示符合菜单->设置->SD卡和电话存储号码的号码,请使用以下代码:

    StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
    double sdAvailSize = (double)stat.getAvailableBlocks()
                       * (double)stat.getBlockSize();
    //One binary gigabyte equals 1,073,741,824 bytes.
    double gigaAvailable = sdAvailSize / 1073741824;
    
    以下是获取内部存储大小的方法:

     StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());        
     long blockSize = statFs.getBlockSize();
     long totalSize = statFs.getBlockCount()*blockSize;
     long availableSize = statFs.getAvailableBlocks()*blockSize;
     long freeSize = statFs.getFreeBlocks()*blockSize;
    
    以下是获取外部存储大小(SD卡大小)的方法:


    短笺

    免费区块:

    要删除的块的总数 在文件系统上免费,包括 保留块(不是 可用于正常应用)

    可用区块:

    上可用的块数 文件系统,并可供 应用程序


    以下是如何检测是否安装了SD卡:

     String state = Environment.getExternalStorageState();
     if (Environment.MEDIA_MOUNTED.equals(state)) 
     {
       // We can read and write the media    
     } 
     else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) 
     {
        // We can only read the media     
     } 
     else 
     {
        // No external media
     }
    

    相关文档:

    为了说明弃用和交叉可用性,我制定了以下方法

    public static long sdCardFree_bytes() {
        File   path        = Environment.getExternalStorageDirectory();
        StatFs stat        = new StatFs(path.getPath());
        long   free_memory = 0; //return value is in bytes
    
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
            free_memory = stat.getAvailableBlocksLong() * stat.getBlockSizeLong();
        } else {
            free_memory = stat.getAvailableBlocks() * stat.getBlockSize();
        }
    
        return free_memory;
    }
    
    public static long sdCardUsed_bytes() {
    
        File   path        = Environment.getExternalStorageDirectory();
        StatFs stat        = new StatFs(path.getPath());
        long   free_memory = 0;
    
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
            free_memory = (stat.getBlockCountLong() - stat.getAvailableBlocksLong()) * stat.getBlockSizeLong(); //return value is in bytes
        } else {
            free_memory = (stat.getBlockCount() - stat.getAvailableBlocks()) * stat.getBlockSize(); //return value is in bytes
        }
    
        return free_memory;
    }
    
    public static long sdCardTotal_bytes() {
    
        File   path        = Environment.getExternalStorageDirectory();
        StatFs stat        = new StatFs(path.getPath());
        long   free_memory = 0;
    
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
            free_memory = stat.getBlockCountLong() * stat.getBlockSizeLong(); //return value is in bytes
        } else {
            free_memory = stat.getBlockCount() * stat.getBlockSize(); //return value is in bytes
        }
    
        return free_memory;
    }
    

    您可以使用
    getTotalSpace()
    getFreeSpace()
    getUsableSpace()


    有趣的解释,非常有用我来这里是因为
    getAvailableBlocks()
    不推荐使用。从KitKat开始就不起作用了。如果尺寸太大,这些值似乎为负值。因此,建议使用
    getAvailableBlocksLong()
    。freeSize是以MB还是以字节为单位?
     String state = Environment.getExternalStorageState();
     if (Environment.MEDIA_MOUNTED.equals(state)) 
     {
       // We can read and write the media    
     } 
     else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) 
     {
        // We can only read the media     
     } 
     else 
     {
        // No external media
     }
    
    public static long sdCardFree_bytes() {
        File   path        = Environment.getExternalStorageDirectory();
        StatFs stat        = new StatFs(path.getPath());
        long   free_memory = 0; //return value is in bytes
    
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
            free_memory = stat.getAvailableBlocksLong() * stat.getBlockSizeLong();
        } else {
            free_memory = stat.getAvailableBlocks() * stat.getBlockSize();
        }
    
        return free_memory;
    }
    
    public static long sdCardUsed_bytes() {
    
        File   path        = Environment.getExternalStorageDirectory();
        StatFs stat        = new StatFs(path.getPath());
        long   free_memory = 0;
    
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
            free_memory = (stat.getBlockCountLong() - stat.getAvailableBlocksLong()) * stat.getBlockSizeLong(); //return value is in bytes
        } else {
            free_memory = (stat.getBlockCount() - stat.getAvailableBlocks()) * stat.getBlockSize(); //return value is in bytes
        }
    
        return free_memory;
    }
    
    public static long sdCardTotal_bytes() {
    
        File   path        = Environment.getExternalStorageDirectory();
        StatFs stat        = new StatFs(path.getPath());
        long   free_memory = 0;
    
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
            free_memory = stat.getBlockCountLong() * stat.getBlockSizeLong(); //return value is in bytes
        } else {
            free_memory = stat.getBlockCount() * stat.getBlockSize(); //return value is in bytes
        }
    
        return free_memory;
    }
    
    import java.io.File;
    import android.util.Log;
    ....
    File f = getMyFile();
    Log.d("MyApp", f.getTotalSpace()+"");
    ....
    
     Use the
    following code
    it may
    help
    
    public void getInternalmemorySize() {
        StatFs stat_fs = new StatFs(Environment.getExternalStorageDirectory().getPath());
        double avail_sd_space = (double) stat_fs.getAvailableBlocksLong() * (double) stat_fs.getBlockSizeLong();
        double GB_Available = (avail_sd_space / 1073741824);
        double GBTotal = ((double) stat_fs.getBlockCountLong() * (double) stat_fs.getBlockSizeLong()) / 1073741824;
        SDCardCheck();
        Log.e("Memory", "Available MB  Internal: " + GB_Available + "---" + GBTotal);
    }
    
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    static String getExternalSdCardSize() {
        File storage = new File("/storage");
        String external_storage_path = "";
        String size = "";
    
        if (storage.exists()) {
            File[] files = storage.listFiles();
    
            for (File file : files) {
                if (file.exists()) {
                    try {
                        if (Environment.isExternalStorageRemovable(file)) {
                            // storage is removable
                            external_storage_path = file.getAbsolutePath();
                            break;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.e("TAG", e.toString());
                    }
                }
            }
        }
    
        if (!external_storage_path.isEmpty()) {
            File external_storage = new File(external_storage_path);
            if (external_storage.exists()) {
                size = totalSize(external_storage);
            }
        }
        return size;
    }
    
    private static String totalSize(File file) {
        StatFs stat = new StatFs(file.getPath());
        long blockSize, totalBlocks;
        long avaiblockSize, availableBlocks;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            blockSize = stat.getBlockSizeLong();
            totalBlocks = stat.getBlockCountLong();
            avaiblockSize = stat.getAvailableBlocksLong();
            availableBlocks = stat.getBlockSizeLong();
        } else {
            blockSize = stat.getBlockSize();
            totalBlocks = stat.getBlockCount();
            avaiblockSize = stat.getAvailableBlocks();
            availableBlocks = stat.getBlockSize();
        }
        Log.e("Memory", "Memory--external--" + (double) (blockSize * totalBlocks) / 1073741824 + "---" + (double) (avaiblockSize * availableBlocks) / 1073741824);
        return formatSize(totalBlocks * blockSize);
    }
    
    private static String formatSize(long size) {
        String suffix = null;
    
        if (size >= 1024) {
            suffix = "KB";
            size /= 1024;
            if (size >= 1024) {
                suffix = "MB";
                size /= 1024;
            }
        }
        size = size / 1024;
        StringBuilder resultBuilder = new StringBuilder(Long.toString(size));
    
        int commaOffset = resultBuilder.length() - 3;
        while (commaOffset > 0) {
            resultBuilder.insert(commaOffset, ',');
            commaOffset -= 3;
        }
    
        if (suffix != null)
            resultBuilder.append(suffix);
        return resultBuilder.toString();
    }
    
    Ther is
    some calculation
    behalf of
    these Methods.
    
    StructStatVfs[
    f_bavail=81523,
    f_bfree=81523,
    f_blocks=242304,
    f_bsize=32768,
    f_favail=0,
    f_ffree=0,
    f_files=0,
    f_flag=1038,
    f_frsize=32768,
    f_fsid=0,
    f_namemax=1530
            ]
    
    StructStatVfs[
    f_bavail=1633375,
    f_bfree=1641567,
    f_blocks=3134770,
    f_bsize=4096,
    f_favail=767939,
    f_ffree=767939,
    f_files=804672,
    f_flag=1038,
    f_frsize=4096,
    f_fsid=0,
    f_namemax=255
            ]
    
    
    Internal-
    
            3134770*4096/1024*1024*1024=11.957.10 1633375*4096/1024*1024*1024=6.23
    
    External-
            81523*32768/1024*1024*1024=2.487 242304*32768/1024*1024*1024=7.39