web-dev-qa-db-fra.com

comment vérifier le stockage interne et externe s'il existe

Comment savoir s'il existe un stockage interne et externe dans Android de manière pragmatique? est-ce que quelqu'un sait comment vérifier le stockage interne et externe

merci d'avance

38
oczdref

c'est déjà expliqué dans documentation Android .

Code extrait de la documentation

boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {
    // We can read and write the media
    mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    // We can only read the media
    mExternalStorageAvailable = true;
    mExternalStorageWriteable = false;
} else {
    // Something else is wrong. It may be one of many other states, but all we need
    //  to know is we can neither read nor write
    mExternalStorageAvailable = mExternalStorageWriteable = false;
}
45
ariefbayu

J'ai écrit une petite classe pour vérifier l'état de stockage. Peut-être que cela vous sera utile.

UPDATE: Code nettoyé, commentaires supprimés et rendu de classe statique.

import Android.os.Environment;

public class StorageHelper {

    private static boolean externalStorageReadable, externalStorageWritable;

    public static boolean isExternalStorageReadable() {
        checkStorage();
        return externalStorageReadable;
    }

    public static boolean isExternalStorageWritable() {
        checkStorage();
        return externalStorageWritable;
    }

    public static boolean isExternalStorageReadableAndWritable() {
        checkStorage();
        return externalStorageReadable && externalStorageWritable;
    }

    private static void checkStorage() {
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            externalStorageReadable = externalStorageWritable = true;
        } else if (state.equals(Environment.MEDIA_MOUNTED) || state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
            externalStorageReadable = true;
            externalStorageWritable = false;
        } else {
            externalStorageReadable = externalStorageWritable = false;
        }
    }

}
24
kaolick

Code de la documentation qui a été un peu simplifié depuis les réponses précédentes:

/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}

/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state) ||
        Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        return true;
    }
    return false;
}
2
MarredCheese

Je le fais fonctionner si quelqu'un cherche cela .... cela aidera: D

try {
            File dir = new File("/mnt/");
            File[] dirs = dir.listFiles();
            for (File _tempDIR : dirs) {
                String sdCard = _tempDIR.getAbsolutePath().toString();

                File file = new File(sdCard + "/"
                        + Environment.DIRECTORY_DOWNLOADS);
                File[] files = file.listFiles();
                if (files != null) {
                    for (int i = 0; i < files.length; i++) {
                        String _temp = files[i].getAbsoluteFile().getName()
                                .toString();/*Your code, and what you want to find, from all the Sdcard, internal and external. Everything mounted will be found :D*/
1
Danny Fallas
File f = new File("/mnt/sdcard/ext_sd");
    if (f.exists()) {
        // Do Whatever you want sdcard exists
    }
    else{
        Toast.makeText(MainActivity.this, "Sdcard not Exists", Toast.LENGTH_SHORT).show();
    }
0
Happy Singh