检查 SDCard 是否存在,布尔值始终为真
在我的启动画面中,我想检查手机是否有 SDC 卡.布尔语句如下:
In my splash screen, I want to check if the phone has a SDCard. The Boolean statement is beneath:
Boolean isSDPresent = android.os.Environment.getExternalStorageState()
.equals(android.os.Environment.MEDIA_MOUNTED );
所以,如果我的手机插槽中有 SDCard,这个布尔值将返回 true,到目前为止一切顺利.当我从设置菜单转到卸载 SDCard"并删除 SDCard,然后杀死应用程序并再次启动它时,布尔值也将为真..
So, if I have the SDCard in the slot on my phone, this boolean will return true, so far so good. When I go to the "Unmount SDCard" from the settings menu, and removes the SDCard, then kill the app and launching it again, the boolean will also be true..
如果我在卸载和移除 sdcard 后启动 Astro File Manager
,我仍然可以访问 /mnt/sdcard
路径,为什么?
And if I launches the Astro File Manager
after unmounting and removing the sdcard, I can still access the /mnt/sdcard
path, why?
我怎样才能做到这一点?
How can I manage to accomplish this?
提前致谢!
编辑
使用以下代码进行测试:
Testing with the following code:
File path = Environment.getExternalStorageDirectory();
String pathS = path.getPath();
当 SDCard 在插槽中时,pathS
包含 mnt/sdcard
,但是当我移除 SDCard 时 pathS
仍然是 /mnt/sdcard ...
When the SDCard is in the slot, the pathS
contains mnt/sdcard
, but when I removes the SDCard the pathS
is still /mnt/sdcard
...
我发现手机,比如三星的 Galaxy 手机,有 /mnt/sdcard
指向内部存储器而不是外置 SD 卡如预期.
I've found that phones, like the Galaxy phones from Samsung, have /mnt/sdcard
point to internal memory and not the external SD card as expected.
可以知道Environment.getExternalStorageDirectory()返回的路径) 实际上是外部 SD 卡,调用 Environment.isExternalStorageRemovable()
You can know if the path returned by Environment.getExternalStorageDirectory() is actually the external SD card with a call to Environment.isExternalStorageRemovable()
只是想从文档中为 getExternalStorageDirectory() 添加这个重要的注释:
Just wanted to add from the docs for getExternalStorageDirectory() this important note:
注意:不要被这里的外部"一词所迷惑.这个目录最好将其视为媒体/共享存储.它是一个文件系统可以保存相对大量的数据,并且可以共享所有应用程序(不强制执行权限).传统上这是SD 卡,但它也可以作为内置存储实现与受保护的内部存储器不同的设备,并且可以作为文件系统安装在计算机上.
Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.