ver tarjeta samsung raiz que miscelaneos memoria los las interna guardar fotos ficheros externa directorio cómo como celular borrar archivos activar java android

java - samsung - ¿Cómo verificar el espacio disponible en el dispositivo Android? en la tarjeta SD?



ficheros en android memoria externa (9)

Además, si desea verificar el espacio disponible en la memoria interna, use:

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

...

Esta pregunta ya tiene una respuesta aquí:

¿Cómo verifico cuánto MB o GB queda en el dispositivo Android? Estoy usando JAVA y Android SDK 2.0.1.

¿Hay algún servicio del sistema que exponga algo como esto?


En función de this respuesta, se agregó compatibilidad con la versión de 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); } @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) private static long getAvailableBytes(StatFs stat) { return stat.getBlockSizeLong() * stat.getAvailableBlocksLong(); }


Espero que este código pueda ayudar a otros. Probado trabajando bien. Gracias al miembro de arriba para aclararlo.

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


He diseñado algunas funciones listas para usar para tener espacio disponible en diferentes unidades. Puede utilizar estos métodos simplemente copiando cualquiera de ellos en su proyecto.

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


La respuesta de Yaroslav dará el tamaño de la tarjeta SD, no el espacio disponible. El getAvailableBlocks() de getAvailableBlocks() devolverá la cantidad de bloques que aún son accesibles para los programas normales. Aquí está la función que estoy 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); }

El código anterior hace referencia a algunas funciones en desuso desde el 13 de agosto de 2014. A continuación, reproduzco una versión actualizada:

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


Prueba ese 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);

Desde allí

UPD: getBlockCount () - devuelve el tamaño de la tarjeta SD; getAvailableBlocks () - devuelve la cantidad de bloques que aún son accesibles para los programas normales (gracias a Joe)


Se introdujeron nuevos métodos desde API versión 18.

Utilicé algo así para la estimación del tamaño de la memoria caché de disco grande (para el caché descargador Picasso OkHttp). El método Helper fue así:

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


Google tiene información sobre esto en la página de inicio : consulte Espacio libre de consulta. Dicen que puede verificar el espacio disponible mediante getFreeSpace() pero afirman que esto es impreciso y que debe esperar un poco menos de espacio libre que este. Ellos dicen:

Si el número devuelto es de unos pocos MB más que el tamaño de los datos que desea guardar, o si el sistema de archivos está lleno en menos del 90%, entonces es seguro proceder. De lo contrario, probablemente no deberías escribir en el almacenamiento.

También dan el consejo de que a menudo es más útil no verificar el espacio libre en absoluto y simplemente try catch un error:

No es necesario que verifique la cantidad de espacio disponible antes de guardar su archivo. En su lugar, puede intentar escribir el archivo de inmediato y luego detectar una IOException si se produce. Es posible que deba hacer esto si no sabe exactamente cuánto espacio necesita. Por ejemplo, si cambia la codificación del archivo antes de guardarlo convirtiendo una imagen PNG a JPEG, no sabrá de antemano el tamaño del archivo.

Recomendaría que solo para tamaños de archivo muy grandes, debe verificar el almacenamiento disponible de antemano para no perder tiempo descargando o creando un archivo que, obviamente, es demasiado grande para contener. En ambos casos, siempre debe usar try catch por lo que creo que el único argumento para verificar el espacio libre disponible de antemano es si el uso innecesario de los recursos y el tiempo es demasiado.


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