有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (6) 个答案

  1. # 1 楼答案

    此外,如果要检查内部内存上的可用空间,请使用:

    File path = Environment.getDataDirectory();
    StatFs stat = new StatFs(path.getPath());
    

  2. # 2 楼答案

    自API版本18起引入了新方法

    我使用类似的方法估算大磁盘缓存大小(用于毕加索OkHttp下载器缓存)。助手方法如下所示:

    private static final String BIG_CACHE_PATH = "my-cache-dir";
    private static final float  MAX_AVAILABLE_SPACE_USE_FRACTION = 0.9f;
    private static final float  MAX_TOTAL_SPACE_USE_FRACTION     = 0.25f;
    
    static File createDefaultCacheDirExample(Context context) {
        File cache = new File(context.getApplicationContext().getCacheDir(), BIG_CACHE_PATH);
        if (!cache.exists()) {
            cache.mkdirs();
        }
        return cache;
    }
    
    /**
     * Calculates minimum of available or total fraction of disk space
     * 
     * @param dir
     * @return space in bytes
     */
    @SuppressLint("NewApi")
    static long calculateAvailableCacheSize(File dir) {
        long size = 0;
        try {
            StatFs statFs = new StatFs(dir.getAbsolutePath());
            int sdkInt = Build.VERSION.SDK_INT;
            long totalBytes;
            long availableBytes;
            if (sdkInt < Build.VERSION_CODES.JELLY_BEAN_MR2) {
                int blockSize = statFs.getBlockSize();
                availableBytes = ((long) statFs.getAvailableBlocks()) * blockSize;
                totalBytes = ((long) statFs.getBlockCount()) * blockSize;
            } else {
                availableBytes = statFs.getAvailableBytes();
                totalBytes = statFs.getTotalBytes();
            }
            // Target at least 90% of available or 25% of total space
            size = (long) Math.min(availableBytes * MAX_AVAILABLE_SPACE_USE_FRACTION, totalBytes * MAX_TOTAL_SPACE_USE_FRACTION);
        } catch (IllegalArgumentException ignored) {
            // ignored
        }
        return size;
    }
    
  3. # 3 楼答案

    我设计了一些现成的功能,以获得不同单元的可用空间。只需将其中任何一种方法复制到项目中,即可使用这些方法

    /**
     * @return Number of bytes available on External storage
     */
    public static long getAvailableSpaceInBytes() {
        long availableSpace = -1L;
        StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
        availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
    
        return availableSpace;
    }
    
    
    /**
     * @return Number of kilo bytes available on External storage
     */
    public static long getAvailableSpaceInKB(){
        final long SIZE_KB = 1024L;
        long availableSpace = -1L;
        StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
        availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
        return availableSpace/SIZE_KB;
    }
    /**
     * @return Number of Mega bytes available on External storage
     */
    public static long getAvailableSpaceInMB(){
        final long SIZE_KB = 1024L;
        final long SIZE_MB = SIZE_KB * SIZE_KB;
        long availableSpace = -1L;
        StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
        availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
        return availableSpace/SIZE_MB;
    }
    
    /**
     * @return Number of gega bytes available on External storage
     */
    public static long getAvailableSpaceInGB(){
        final long SIZE_KB = 1024L;
        final long SIZE_GB = SIZE_KB * SIZE_KB * SIZE_KB;
        long availableSpace = -1L;
        StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
        availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
        return availableSpace/SIZE_GB;
    }
    
  4. # 4 楼答案

    雅罗斯拉夫的回答将给出SD卡的大小,而不是可用空间。StatFs的getAvailableBlocks()将返回正常程序仍可访问的块数。以下是我正在使用的函数:

    public static float megabytesAvailable(File f) {
        StatFs stat = new StatFs(f.getPath());
        long bytesAvailable = (long)stat.getBlockSize() * (long)stat.getAvailableBlocks();
        return bytesAvailable / (1024.f * 1024.f);
    }
    

    上述代码引用了截至2014年8月13日的一些弃用函数。我在下面复制了一个更新版本:

    public static float megabytesAvailable(File f) {
        StatFs stat = new StatFs(f.getPath());
        long bytesAvailable = 0;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2)
            bytesAvailable = (long) stat.getBlockSizeLong() * (long) stat.getAvailableBlocksLong();
        else
            bytesAvailable = (long) stat.getBlockSize() * (long) stat.getAvailableBlocks();
        return bytesAvailable / (1024.f * 1024.f);
    }
    
  5. # 5 楼答案

    试试this code

    StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
    
    long bytesAvailable = (long)stat.getBlockSize() *(long)stat.getBlockCount();
    long megAvailable   = bytesAvailable / 1048576;
    
    System.out.println("Megs: " + megAvailable);
    

    更新:

    getBlockCount()-返回SD卡的大小

    getAvailableBlocks()-返回正常程序仍可访问的块数(谢谢Joe)

  6. # 6 楼答案

    基于this答案,添加了对Android版本的支持<;十八

    public static float megabytesAvailable(File file) {
        StatFs stat = new StatFs(file.getPath());
        long bytesAvailable;
        if(Build.VERSION.SDK_INT >= 18){
            bytesAvailable = getAvailableBytes(stat);
        }
        else{
            //noinspection deprecation
            bytesAvailable = stat.getBlockSize() * stat.getAvailableBlocks();
        }
    
        return bytesAvailable / (1024.f * 1024.f);
    }
    
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    private static long getAvailableBytes(StatFs stat) {
        return stat.getBlockSizeLong() * stat.getAvailableBlocksLong();
    }