2011-09-15 11 views

risposta

120
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 
} 
+3

come controllare la memoria libera della sdcard? – naresh

+1

grazie sta funzionando – naresh

+40

ma restituisce vero se il telefono ha memoria inbuild..non è la risposta corretta – User10001

12

Usa Environment.getExternalStorageState() come descritto in "Using the External Storage".

Per ottenere lo spazio disponibile sullo storage esterno, usare 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(); 
3
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); 
} 
5

ho scritto un po 'di classe per che la verifica dello stato di memorizzazione. Forse è di qualche utilità per te.

+0

questo file aiuta a rilevare la disponibilità della scheda SD? –

+0

@PankajNimgade Questa classe ti aiuta a verificare se l'archiviazione esterna è disponibile e/o scrivibile. La memoria esterna può essere una scheda SD o una memoria incorporata come nei dispositivi nexus. – kaolick

+0

è possibile verificare specificamente la "scheda SD"?, Grazie in anticipo –

4

L'ho modificato in modo tale che se esiste una scheda SD, imposta il percorso lì. In caso contrario, lo imposta nella directory interna.

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

ho creato una classe per verificare se la cartella sulla scheda SD è disponibile o meno:

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

Questo semplice metodo è lavorato per me. Testato su tutti i tipi di dispositivi.

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

risposta accettata non funziona per me

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

Nel caso in cui se il dispositivo è dotato di un in deposito, restituisce vero; La mia soluzione è quella per controllare il conteggio delle directory dei file esterni, se ce n'è più di una, il dispositivo ha la sdcard. Funziona e l'ho testato per diversi dispositivi.

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

È possibile verificare se esterna scheda SD rimovibile è disponibile come questo

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; 

} 

Questo funziona perfettamente come ho testato.

Problemi correlati