如何检查是否一个SD卡插入到平板电脑或不:Android的? [英] How to check if a SD Card has been inserted to the tablet or not : Android?

查看:125
本文介绍了如何检查是否一个SD卡插入到平板电脑或不:Android的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个简单的应用程序,这应该能够访问内部存储器中的文件和以及来自外部存储(可移动卡)之类的Micro SD卡(当用户插入SD卡)。

(没有内置SD卡附带的设备,我知道它可以通过访问 Environment.getExternalStorageDirectory()

是否有可能发现如果用户插入SD卡的设备? 如果是的话,是有可能得到的SD卡的路径?

我发现,硬编码的路径是不是一个好的选择,COS不同的设备具有SD卡由用户插入不同的路径。

任何帮助是非常AP preciated。 谢谢你。

解决方案
  

(没有内置SD卡附带的设备,我知道这可能是   使用Environment.getExternalStorageDirectory访问())

机器人考虑这两个可移动存储介质(如SD卡)或内部(不可移动)存储作为外部存储仅

以下是形式developer.android.com

  

每一个Android兼容设备支持共享外部存储   你可以用它来保存文件。这可以是一个可移动存储介质   (如SD卡)或内部(不可移动)的存储。档   保存到外部存储是世界可读和可修改   由用户时它们使USB大容量存储到上一个传输文件   计算机。

要检查SD卡的可用性,您可以使用下面的code。

 私人布尔isExternalStorageAvailable(){

        字符串状态= Environment.getExternalStorageState();
        布尔mExternalStorageAvailable = FALSE;
        布尔mExternalStorageWriteable = FALSE;

        如果(Environment.MEDIA_MOUNTED.equals(州)){
            //我们可以读取和写入的媒体
            mExternalStorageAvailable = mExternalStorageWriteable = TRUE;
        }否则,如果(Environment.MEDIA_MOUNTED_READ_ONLY.equals(州)){
            //我们只能读取介质
            mExternalStorageAvailable = TRUE;
            mExternalStorageWriteable = FALSE;
        } 其他 {
            //别的东西是错误的。这可能是很多其他的国家之一,但
            //我们所需要的
            //知道的是,我们既不能读也不能写
            mExternalStorageAvailable = mExternalStorageWriteable = FALSE;
        }

        如果(mExternalStorageAvailable == TRUE
                &功放;&安培; mExternalStorageWriteable ==真){
            返回true;
        } 其他 {
            返回false;
        }
    }
 

请阅读<一个href="http://developer.android.com/guide/topics/data/data-storage.html#filesExternal">http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

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.

解决方案

(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

这篇关于如何检查是否一个SD卡插入到平板电脑或不:Android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆