How to check if a SD Card has been inserted to the tablet or not : Android?

[亡魂溺海] 提交于 2019-12-29 06:30:32

问题


I am working on a simple app, which should be able to access files from internal storage and as well as from external storage (Removable cards) like Micro SD cards (when an user inserts a SDCARD).

(Not the internal sdcard which comes with the device, I know it can be accessed using Environment.getExternalStorageDirectory())

Is it possible to find out if an user has inserted a sdcard to the device? If yes, Is it possible to get the path of that SD CARD?

I found that hard coding the path was not a good option, cos different devices has different paths for sdcard inserted by user.

Any help is very much appreciated. Thank you.


回答1:


(Not the internal sdcard which comes with the device, I know it can be accessed using Environment.getExternalStorageDirectory() )

Android consider Both removable storage media (such as an SD card) or an internal (non-removable) storage as external storage only.

Following is form developer.android.com

Every Android-compatible device supports a shared "external storage" that you can use to save files. This can be a removable storage media (such as an SD card) or an internal (non-removable) storage. Files saved to the external storage are world-readable and can be modified by the user when they enable USB mass storage to transfer files on a computer.

To check SDCard availability you can use following code.

private boolean isExternalStorageAvailable() {

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

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

        if (mExternalStorageAvailable == true
                && mExternalStorageWriteable == true) {
            return true;
        } else {
            return false;
        }
    }

Please read http://developer.android.com/guide/topics/data/data-storage.html#filesExternal




回答2:


Try:

Boolean mSDcheck = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);



回答3:


The simplest solution i am going to show you here. if you want to show whether sdcard is inserted or not, Then just try to find the files of the external sdcard path, code is here:

File file = new File("/mnt/extSdCard");
         try
         {
         File list[] = file.listFiles();
         Toast.makeText(getApplicationContext(), "Yes sdcard is mounted, file count "+list.length, Toast.LENGTH_LONG).show();
         }
         catch(NullPointerException o)
         {
         Toast.makeText(getApplicationContext(), "Sorry no sdcard is mounted:", Toast.LENGTH_LONG).show();
         }



回答4:


"External storage" is not necessarily an SD card but can be mounted internal memory. The method getExternalStorageState() on much new age Android devices with SD card checks the internal memory but not SD card. The more simple option would be to use getExternalMediaDirs() method within Context class. It returns an array of files with absolute path and can be used to detect the presence of SD card.



来源:https://stackoverflow.com/questions/12721827/how-to-check-if-a-sd-card-has-been-inserted-to-the-tablet-or-not-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!