简体   繁体   中英

Detect removable external storage in Android

As I know, there are 3 kinds of storage,

  1. Internal Storage : Its private and no one can access to it.
    For example: File internalFile = new File(getFilesDir(), "MyFile.txt");
  2. External Storage : It is not removable.
    For example: File externalFile = new File(Environment.getExternalStorageDirectory(), "MyFile.txt");
  3. Secondary External Storage : It can be mounted or unmounted by the user.

My question is for third type of storage. How can I access to secondary SD card? I have searched for hours but I did not find the answer.

There are only 2 types,

  1. Internal Storage
  2. External storage , which can be removable (SD card) or non-removable.

Check this guide for more details. I hope it has all that you need.

You can detect the storage state, as specified in the guide, using the following code

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

To detect sd card availability you can do it on this way

    boolean mExtStorage = false;
    boolean mExtWriteable = false;
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        mExtStorage = mExtWriteable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        mExtStorage = true;
        mExtWriteable = false;
    } else {
        mExtStorage = mExtWriteable = false;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM