how to check internal and external storage if exist

后端 未结 5 590
说谎
说谎 2020-11-29 04:03

How do i know if there are internal and external storage in android pragmatically? does anyone how to know how to check both internal and external storage

thanks in

相关标签:
5条回答
  • 2020-11-29 04:22

    I got it working if someone is searching for this.... it will help :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*/
    
    0 讨论(0)
  • 2020-11-29 04:25

    Code from the documentation that's been simplified a bit since previous answers:

    /* 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;
    }
    
    0 讨论(0)
  • 2020-11-29 04:32

    I wrote a little class for that checking the storage state. Maybe it's of some use for you.

    UPDATE: Cleaned up code, removed comments and made class static.

    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;
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-11-29 04:44

    it's already explained in android documentation.

    Code taken from 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;
    }
    
    0 讨论(0)
  • 2020-11-29 04:46
    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 讨论(0)
提交回复
热议问题