Подтвердить что ты не робот

Как проверить внутреннее и внешнее хранилище, если они существуют

Как узнать, есть ли внутреннее и внешнее хранилище в android прагматично? кто-нибудь знает, как проверить как внутреннее, так и внешнее хранилище

заблаговременно

4b9b3361

Ответ 1

это уже объяснено в документации по Android.

Код, взятый из документации

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

Ответ 2

Я написал небольшой класс для проверки состояния хранилища. Может быть, это для вас какое-то использование.

UPDATE: Очищенный код, удаленные комментарии и статический класс.

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

}

Ответ 3

Я работал, если кто-то ищет это... это поможет: 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*/

Ответ 4

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