ver tarjeta raiz moto micro memoria los llena interna externa expandible directorio comprobar como celular cambiar archivos almacenamiento activar android sd-card

android - tarjeta - moto e memoria interna llena



Compruebe si la tarjeta SD está disponible o no programáticamente (9)

Creé una clase para verificar si la carpeta de la tarjeta SD está disponible o no:

public class GetFolderPath { static String folderPath; public static String getFolderPath(Context context) { if (isSdPresent() == true) { try { File sdPath = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/FolderName"); if(!sdPath.exists()) { sdPath.mkdirs(); folderPath = sdPath.getAbsolutePath(); } else if (sdPath.exists()) { folderPath = sdPath.getAbsolutePath(); } } catch (Exception e) { } folderPath = Environment.getExternalStorageDirectory().getPath()+"/FolderName/"; } else { try { File cacheDir=new File(context.getCacheDir(),"FolderName/"); if(!cacheDir.exists()) { cacheDir.mkdirs(); folderPath = cacheDir.getAbsolutePath(); } else if (cacheDir.exists()) { folderPath = cacheDir.getAbsolutePath(); } } catch (Exception e){ } } return folderPath; } public static boolean isSdPresent() { return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); } }

Mi aplicación funciona para móviles que solo tienen una tarjeta SD. Por lo tanto, programáticamente quiero verificar si la tarjeta SD está disponible o no y cómo encontrar el espacio libre de la tarjeta SD. ¿Es posible?

Si es así, ¿cómo lo hago?


Escribí una pequeña clase para verificar el estado de almacenamiento. Tal vez te sirva de algo.

import android.os.Environment; /** * Checks the state of the external storage of the device. * * @author kaolick */ public class StorageHelper { // Storage states private boolean externalStorageAvailable, externalStorageWriteable; /** * Checks the external storage''s state and saves it in member attributes. */ private void checkStorage() { // Get the external storage''s state String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { // Storage is available and writeable externalStorageAvailable = externalStorageWriteable = true; } else if (state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) { // Storage is only readable externalStorageAvailable = true; externalStorageWriteable = false; } else { // Storage is neither readable nor writeable externalStorageAvailable = externalStorageWriteable = false; } } /** * Checks the state of the external storage. * * @return True if the external storage is available, false otherwise. */ public boolean isExternalStorageAvailable() { checkStorage(); return externalStorageAvailable; } /** * Checks the state of the external storage. * * @return True if the external storage is writeable, false otherwise. */ public boolean isExternalStorageWriteable() { checkStorage(); return externalStorageWriteable; } /** * Checks the state of the external storage. * * @return True if the external storage is available and writeable, false * otherwise. */ public boolean isExternalStorageAvailableAndWriteable() { checkStorage(); if (!externalStorageAvailable) { return false; } else if (!externalStorageWriteable) { return false; } else { return true; } } }


Este sencillo método me funciona. Probado en todo tipo de dispositivos.

public boolean externalMemoryAvailable() { if (Environment.isExternalStorageRemovable()) { //device support sd card. We need to check sd card availability. String state = Environment.getExternalStorageState(); return state.equals(Environment.MEDIA_MOUNTED) || state.equals( Environment.MEDIA_MOUNTED_READ_ONLY); } else { //device not support sd card. return false; } }


La respuesta aceptada no funciona para mí

Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

En caso de que el dispositivo tenga un almacenamiento incorporado, devuelve verdadero; Mi solución es que para verificar el recuento de directorios de archivos externos, si hay más de uno, el dispositivo tiene tarjeta SD. Funciona y lo probé para varios dispositivos.

public static boolean hasRealRemovableSdCard(Context context) { return ContextCompat.getExternalFilesDirs(context, null).length >= 2; }


Lo modifiqué de tal manera que si existe una tarjeta SD, establece el camino allí. Si no, lo establece en el directorio interno.

Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); if(isSDPresent) { path = theAct.getExternalCacheDir().getAbsolutePath() + "/GrammarFolder"; } else { path = theAct.getFilesDir() + "/GrammarFolder"; }


Puede verificar si la tarjeta sd extraíble externa está disponible como esta

public static boolean externalMemoryAvailable(Activity context) { File[] storages = ContextCompat.getExternalFilesDirs(context, null); if (storages.length > 1 && storages[0] != null && storages[1] != null) return true; else return false; }

Esto funciona perfectamente ya que lo he probado.


Use Environment.getExternalStorageState() como se describe en "Uso del almacenamiento externo" .

Para obtener espacio disponible en el almacenamiento externo, use StatFs :

// do this only *after* you have checked external storage state: File extdir = Environment.getExternalStorageDirectory(); File stats = new StatFs(extdir.getAbsolutePath()); int availableBytes = stats.getAvailableBlocks() * stats.getBlockSize();


void updateExternalStorageState() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { mExternalStorageAvailable = mExternalStorageWriteable = true; } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { mExternalStorageAvailable = true; mExternalStorageWriteable = false; } else { mExternalStorageAvailable = mExternalStorageWriteable = false; } handleExternalStorageState(mExternalStorageAvailable, mExternalStorageWriteable); }


Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); Boolean isSDSupportedDevice = Environment.isExternalStorageRemovable(); if(isSDSupportedDevice && isSDPresent) { // yes SD-card is present } else { // Sorry }