Device claims external storage, yet I have no SD card

最后都变了- 提交于 2019-11-29 14:59:17

getExternalStorageDirectory doesn't always return the SDCard.

Google doc says :

"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."

There might be chances that "/mnt/sdcard" refers to the inbuilt storage for your phone.

It is better to check the return path from the getExternalStorageDirectory method whether it is external removable storage or not.

You can use Environment.isExternalStorageRemovable() for checking it.

Android will always report one mounted hardware memory (if any available) as External Storage.

That memory can be:

  1. fitted inside the device by manufacturer (internal memory)
  2. can be an sd card (external memory)

A device can even have both, but Android will report only one of them (mostly internal one).

A simple way to get what is mounted where is adb shell mount.

rootfs on / type rootfs (ro)
tmpfs on /dev type tmpfs (rw,nosuid,mode=755)
devpts on /dev/pts type devpts (rw,mode=600)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
tmpfs on /mnt/asec type tmpfs (rw,mode=755,gid=1000)
tmpfs on /mnt/obb type tmpfs (rw,mode=755,gid=1000)
/dev/block/mtdblock2 on /system type yaffs2 (ro)
/dev/block/mtdblock3 on /data type yaffs2 (rw,nosuid,nodev)
/dev/block/mtdblock1 on /cache type yaffs2 (rw,nosuid,nodev)
/dev/block/vold/179:1 on /mnt/sdcard type vfat (rw,dirsync,nosuid,nodev,noexec,uid=1000,gid=1015,fmask=0702,dmask=0702,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro)
/dev/block/vold/179:1 on /mnt/secure/asec type vfat (rw,dirsync,nosuid,nodev,noexec,uid=1000,gid=1015,fmask=0702,dmask=0702,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro)

Use this code to know if there is a memory card in the device:

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

if(isSDPresent)
{
  // yes SD-card is present
}
else
{
 // Sorry
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!