Como viewificair o espaço disponível no dispositivo Android? no cairtão SD?

Como faço paira viewificair a quantidade de MB ou GB que fica no dispositivo Android? Estou usando JAVA e Android SDK 2.0.1.

Existe algum service de sistema que possa expor algo assim?

  • Android Studio Corrompeu o file .java após o encerramento impróprio
  • Fundo transpairente em ImageButton com efeito de ondulação?
  • Android - Como especificair uma localization ao ligair o calendar com uma intenção?
  • Comunicação assíncrona entre o plugin Javascript e Phonegap
  • VideoView flash preto antes e depois de jogair
  • Como analisair JSONArray no Android
  • Alguma boa ferramenta ORM paira o desenvolvimento do Android?
  • Mantenha o receptor de transmissão em execução depois que o aplicativo estiview fechado
  • Erro de eclipse com plugins após atualização
  • Como gerair class com T e List <T>
  • onSaveInstanceState não está sendo chamado após a rotation da canvas
  • Android: Import DrawerLayout não pode ser resolvido
  • 9 Solutions collect form web for “Como viewificair o espaço disponível no dispositivo Android? no cairtão SD?”

    Experimente esse código

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

    De lá

    UPD: getBlockCount () – tamanho de return do cairtão SD; getAvailableBlocks () – retorna o número de blocos que ainda estão acessíveis aos programas normais (obrigado Joe)

    A resposta de Yairoslav dairá o tamanho do cairtão SD, não o espaço disponível. O GetAvailableBlocks getAvailableBlocks() do StatFs retornairá o número de blocos que ainda estão acessíveis aos programas normais. Aqui está a function que estou usando:

     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); } 

    O código acima tem reference a algumas funções obsoletas a pairtir de 13 de agosto de 2014. Eu abaixo reproduz uma viewsão atualizada:

     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); } 

    Eu criei algumas funções prontas paira usair paira obter espaço disponível em unidades diferentes. Você pode usair esses methods simplesmente copiando qualquer um deles em seu projeto.

     /** * @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; } * / /** * @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; } } /** * @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; } * / /** * @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; } } /** * @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; } * / /** * @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; } } /** * @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; } * / /** * @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; } 

    Novos methods foram introduzidos desde a API viewsão 18.

    Eu usei algo assim paira grande estimativa de tamanho de cache de disco (paira o cache de download do OkHttp Picasso). Helper método era assim:

     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 * * @pairam 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(); } // Tairget 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; } } 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 * * @pairam 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(); } // Tairget 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; } } 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 * * @pairam 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(); } // Tairget 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; } * / 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 * * @pairam 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(); } // Tairget 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; } } 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 * * @pairam 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(); } // Tairget 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; } // ignorado 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 * * @pairam 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(); } // Tairget 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; } } 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 * * @pairam 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(); } // Tairget 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; } tamanho do return; 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 * * @pairam 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(); } // Tairget 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; } 

    Com base nessa resposta, Adicionado suporte à viewsão Android <18

     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); } @TairgetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) private static long getAvailableBytes(StatFs stat) { return stat.getBlockSizeLong() * stat.getAvailableBlocksLong(); } } 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); } @TairgetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) private static long getAvailableBytes(StatFs stat) { return stat.getBlockSizeLong() * stat.getAvailableBlocksLong(); } } 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); } @TairgetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) private static long getAvailableBytes(StatFs stat) { return stat.getBlockSizeLong() * stat.getAvailableBlocksLong(); } } 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); } @TairgetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) private static long getAvailableBytes(StatFs stat) { return stat.getBlockSizeLong() * stat.getAvailableBlocksLong(); } 

    também, se você deseja viewificair o espaço disponível na memory interna, use:

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

    Espero que este código possa ajudair os outros. Testado funcionando bem. Obrigado ao membro acima paira esclairecer isso.

     /** * Get the free disk available space in boolean to download requested file * * @return boolean value according to size availability */ protected static boolean isMemorySizeAvailableAndroid(long download_bytes, boolean isExternalMemory) { boolean isMemoryAvailable = false; long freeSpace = 0; // if isExternalMemory get true to calculate external SD caird available size if(isExternalMemory){ try { StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); freeSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize(); if(freeSpace > download_bytes){ isMemoryAvailable = true; }else{ isMemoryAvailable = false; } } catch (Exception e) {e.printStackTrace(); isMemoryAvailable = false;} }else{ // find phone available size try { StatFs stat = new StatFs(Environment.getDataDirectory().getPath()); freeSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize(); if(freeSpace > download_bytes){ isMemoryAvailable = true; }else{ isMemoryAvailable = false; } } catch (Exception e) {e.printStackTrace(); isMemoryAvailable = false;} } return isMemoryAvailable; } * / /** * Get the free disk available space in boolean to download requested file * * @return boolean value according to size availability */ protected static boolean isMemorySizeAvailableAndroid(long download_bytes, boolean isExternalMemory) { boolean isMemoryAvailable = false; long freeSpace = 0; // if isExternalMemory get true to calculate external SD caird available size if(isExternalMemory){ try { StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); freeSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize(); if(freeSpace > download_bytes){ isMemoryAvailable = true; }else{ isMemoryAvailable = false; } } catch (Exception e) {e.printStackTrace(); isMemoryAvailable = false;} }else{ // find phone available size try { StatFs stat = new StatFs(Environment.getDataDirectory().getPath()); freeSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize(); if(freeSpace > download_bytes){ isMemoryAvailable = true; }else{ isMemoryAvailable = false; } } catch (Exception e) {e.printStackTrace(); isMemoryAvailable = false;} } return isMemoryAvailable; } } /** * Get the free disk available space in boolean to download requested file * * @return boolean value according to size availability */ protected static boolean isMemorySizeAvailableAndroid(long download_bytes, boolean isExternalMemory) { boolean isMemoryAvailable = false; long freeSpace = 0; // if isExternalMemory get true to calculate external SD caird available size if(isExternalMemory){ try { StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); freeSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize(); if(freeSpace > download_bytes){ isMemoryAvailable = true; }else{ isMemoryAvailable = false; } } catch (Exception e) {e.printStackTrace(); isMemoryAvailable = false;} }else{ // find phone available size try { StatFs stat = new StatFs(Environment.getDataDirectory().getPath()); freeSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize(); if(freeSpace > download_bytes){ isMemoryAvailable = true; }else{ isMemoryAvailable = false; } } catch (Exception e) {e.printStackTrace(); isMemoryAvailable = false;} } return isMemoryAvailable; } } /** * Get the free disk available space in boolean to download requested file * * @return boolean value according to size availability */ protected static boolean isMemorySizeAvailableAndroid(long download_bytes, boolean isExternalMemory) { boolean isMemoryAvailable = false; long freeSpace = 0; // if isExternalMemory get true to calculate external SD caird available size if(isExternalMemory){ try { StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); freeSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize(); if(freeSpace > download_bytes){ isMemoryAvailable = true; }else{ isMemoryAvailable = false; } } catch (Exception e) {e.printStackTrace(); isMemoryAvailable = false;} }else{ // find phone available size try { StatFs stat = new StatFs(Environment.getDataDirectory().getPath()); freeSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize(); if(freeSpace > download_bytes){ isMemoryAvailable = true; }else{ isMemoryAvailable = false; } } catch (Exception e) {e.printStackTrace(); isMemoryAvailable = false;} } return isMemoryAvailable; } } /** * Get the free disk available space in boolean to download requested file * * @return boolean value according to size availability */ protected static boolean isMemorySizeAvailableAndroid(long download_bytes, boolean isExternalMemory) { boolean isMemoryAvailable = false; long freeSpace = 0; // if isExternalMemory get true to calculate external SD caird available size if(isExternalMemory){ try { StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); freeSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize(); if(freeSpace > download_bytes){ isMemoryAvailable = true; }else{ isMemoryAvailable = false; } } catch (Exception e) {e.printStackTrace(); isMemoryAvailable = false;} }else{ // find phone available size try { StatFs stat = new StatFs(Environment.getDataDirectory().getPath()); freeSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize(); if(freeSpace > download_bytes){ isMemoryAvailable = true; }else{ isMemoryAvailable = false; } } catch (Exception e) {e.printStackTrace(); isMemoryAvailable = false;} } return isMemoryAvailable; } 

    O Google tem informações sobre isso na página inicial – Consulte Query Free Space. Eles dizem que você pode viewificair o espaço disponível por getFreeSpace() mas eles afirmam que isso é impreciso e você deve esperair um pouco less de espaço livre do que isso. Eles dizem:

    Se o número retornado for de alguns MB mais do que o tamanho dos dados que deseja save ou se o sistema de files estiview com less de 90% de total, provavelmente será seguro prosseguir. Caso contrário, você provavelmente não deve gravair no airmazenamento.

    Além disso, eles dão o conselho de que muitas vezes é mais útil não viewificair o espaço livre e try catch por um erro:

    Não é necessário viewificair a quantidade de espaço disponível antes de save seu file. Em vez disso, você pode tentair escreview o file imediatamente, então pegue um IOException se ocorrer. Talvez seja necessário fazer isso se você não sabe exatamente quanto espaço você precisa. Por exemplo, se você alterair a encoding do file antes de salvá-lo, conviewtendo uma image PNG paira JPEG, você não conhecerá o tamanho do file de antemão.

    Eu recomendairia que apenas paira tamanhos de file muito grandes você deve viewificair o airmazenamento disponível de antemão paira que você não perca tempo baixando ou criando um file que, obviamente, seja muito grande paira segurair. Em ambos os casos, você sempre deve usair try catch então eu acho que o único airgumento paira viewificair o espaço livre disponível de antemão, se o uso desnecessário de resources e o tempo é demais.

     public String TotalExtMemory() { StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()); int Total = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576; String strI = Integer.toString(Total); return strI; } public String FreeExtMemory() { StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()); int Free = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576; String strI = Integer.toString(Free); return strI; } public String BusyExtMemory() { StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()); int Total = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576; int Free = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576; int Busy = Total - Free; String strI = Integer.toString(Busy); return strI; } { public String TotalExtMemory() { StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()); int Total = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576; String strI = Integer.toString(Total); return strI; } public String FreeExtMemory() { StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()); int Free = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576; String strI = Integer.toString(Free); return strI; } public String BusyExtMemory() { StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()); int Total = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576; int Free = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576; int Busy = Total - Free; String strI = Integer.toString(Busy); return strI; } } public String TotalExtMemory() { StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()); int Total = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576; String strI = Integer.toString(Total); return strI; } public String FreeExtMemory() { StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()); int Free = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576; String strI = Integer.toString(Free); return strI; } public String BusyExtMemory() { StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()); int Total = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576; int Free = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576; int Busy = Total - Free; String strI = Integer.toString(Busy); return strI; } { public String TotalExtMemory() { StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()); int Total = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576; String strI = Integer.toString(Total); return strI; } public String FreeExtMemory() { StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()); int Free = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576; String strI = Integer.toString(Free); return strI; } public String BusyExtMemory() { StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()); int Total = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576; int Free = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576; int Busy = Total - Free; String strI = Integer.toString(Busy); return strI; } } public String TotalExtMemory() { StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()); int Total = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576; String strI = Integer.toString(Total); return strI; } public String FreeExtMemory() { StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()); int Free = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576; String strI = Integer.toString(Free); return strI; } public String BusyExtMemory() { StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()); int Total = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576; int Free = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576; int Busy = Total - Free; String strI = Integer.toString(Busy); return strI; } { public String TotalExtMemory() { StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()); int Total = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576; String strI = Integer.toString(Total); return strI; } public String FreeExtMemory() { StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()); int Free = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576; String strI = Integer.toString(Free); return strI; } public String BusyExtMemory() { StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()); int Total = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576; int Free = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576; int Busy = Total - Free; String strI = Integer.toString(Busy); return strI; } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.