当sdcard插入android设备时,为什么我的外部SD卡存在返回false?

时间:2014-12-26 09:26:14

标签: android file sd-card android-sdcard mount

使用这样的代码:

// /mnt/media_rw/udisk/sda4 is my external sdcard path
File sdcard = new File("/mnt/media_rw/udisk/sda4");

Log.d(LOG_TAG, "sdcard: " + sdcard 
                + ", exists: " + sdcard.exists()            // return false
                + ", is directory: " + sdcard.isDirectory() // return false
                + ", can write: " + sdcard.canWrite());     // return false

因为我在电视平台上工作,所以我使用Tera Term工具来执行ls /mnt/media_rw/udisk/sda4,我有文件列表。

而且,这是我的/proc/mounts

rootfs / rootfs ro,relatime 0 0
devtmpfs /dev devtmpfs rw,seclabel,relatime,size=111156k,nr_inodes=22811,mode=755 0 0
tmpfs /dev tmpfs rw,seclabel,nosuid,relatime,mode=755 0 0
devpts /dev/pts devpts rw,seclabel,relatime,mode=600,ptmxmode=000 0 0
proc /proc proc rw,relatime 0 0
sysfs /sys sysfs rw,seclabel,relatime 0 0
selinuxfs /sys/fs/selinux selinuxfs rw,relatime 0 0
debugfs /sys/kernel/debug debugfs rw,relatime 0 0
none /mnt/media_rw tmpfs rw,seclabel,relatime,mode=700,uid=1023,gid=1023 0 0
none /storage tmpfs rw,seclabel,relatime,mode=751,uid=1028,gid=1028 0 0
tmpfs /mnt/secure tmpfs rw,seclabel,relatime,mode=700 0 0
tmpfs /tmp tmpfs rw,seclabel,relatime,size=20480k,mode=771 0 0
tmpfs /mnt/asec tmpfs rw,seclabel,relatime,mode=755,gid=1000 0 0
tmpfs /mnt/obb tmpfs rw,seclabel,relatime,mode=755,gid=1000 0 0
/dev/block/mmcblk0p1 /system ext4 rw,seclabel,relatime,data=ordered 0 0
/dev/block/mmcblk0p2 /data ext4 rw,seclabel,nosuid,nodev,noatime,nomblk_io_submit,errors=panic,data=ordered 0 0
/dev/block/mmcblk0p3 /cache ext4 rw,seclabel,nosuid,nodev,noatime,nomblk_io_submit,errors=panic,data=ordered 0 0
/dev/fuse /mnt/shell/emulated fuse rw,nosuid,nodev,relatime,user_id=1023,group_id=1023,default_permissions,allow_other 0 0
/dev/block/vold/8:4 **/mnt/media_rw/udisk/sda4** vfat rw,dirsync,nosuid,nodev,noexec,relatime,uid=1023,gid=1023,fmask=0007,dmask=0007,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,usefree,utf8,errors=remount-ro 0 0
/dev/fuse /storage/udisk/sda4 fuse rw,nosuid,nodev,relatime,user_id=1023,group_id=1023,default_permissions,allow_other 0 0

我尝试/storage/udisk/sda4/而不是/mnt/media_rw/udisk/sda4,它返回true。这是为什么?

2 个答案:

答案 0 :(得分:0)

检查您的权限(<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />中的AndroidManifest.xml),使用Environment.getExternalStorageDirectory()代替直接路径并尝试执行此功能:

private static boolean isAvilableForRead()
    {
        boolean externalStorageReadable = false;
        String state = Environment.getExternalStorageState();

        if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state) || Environment.MEDIA_MOUNTED.equals(state)) 
        {
            // We can read the media
            externalStorageReadable = true;
        }

        return externalStorageReadable;
    }

如果返回false,则不是Android SDK问题。

答案 1 :(得分:0)

  

试试此代码

文件文件=新文件(Environment.getExternalStorageDirectory()+ File.separator +&#34; FolderName&#34;);

//如果obj与此对象相同,则返回true,否则返回false。

Log.d(LOG_TAG,&#34;存在文件:&#34; + file.exists());