android 获得手机外插SD卡 存储路径和存储空间
獲取存儲路徑:
/**** <p>* Description: 獲取手機外插SD 存儲路徑* <p>* @date 2017-4-18* @author * @param* @return*/public static String getOuterSDPath(){String sdcard_path = null;String sd_default = Environment.getExternalStorageDirectory().getAbsolutePath();LogEx.i("DownloadPathUtil sd_default", sd_default);// sd_default = /storage/emulated/0if (sd_default.endsWith("/")){sd_default = sd_default.substring(0, sd_default.length() - 1);}// 得到路徑try{Runtime runtime = Runtime.getRuntime();Process proc = runtime.exec("mount");InputStream is = proc.getInputStream();InputStreamReader isr = new InputStreamReader(is);String line;BufferedReader br = new BufferedReader(isr);while ((line = br.readLine()) != null){LogEx.i("DownloadPathUtil line:", line);if (line.contains("secure"))continue;if (line.contains("asec"))continue;if (line.contains("fat") && line.contains("/mnt/")){String columns[] = line.split(" ");if (columns != null && columns.length > 1){if (sd_default.trim().equals(columns[1].trim())){continue;}sdcard_path = columns[1];}}else if (line.contains("fuse") && line.contains("/mnt/")){String columns[] = line.split(" ");if (columns != null && columns.length > 1){if (sd_default.trim().equals(columns[1].trim())){continue;}sdcard_path = columns[1];}}else if (line.contains("extSdCard")){String columns[] = line.split(" ");if (columns != null && columns.length > 1){if (sd_default.trim().equals(columns[1].trim())){continue;}sdcard_path = columns[1];}}}}catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}LogEx.i("DownloadPathUtil", sdcard_path);return sdcard_path;}Line 8065: 05-16 15:53:19.826 I/DownloadPathUtil sd_default(25251): 2017年5月16日 PM3:53:19 ? ?F[DownloadPathUtil] ? ?L[433][getOuterSDPath] ? ? ?/storage/emulated/0
Line 8065: 05-16 15:53:19.826 I/DownloadPathUtil sd_default(25251): 2017年5月16日 PM3:53:19 ? ?F[DownloadPathUtil] ? ?L[433][getOuterSDPath] ? ? ? /storage/emulated/0 ? ?這是打印的日志
發現 ??
String sd_default = Environment.getExternalStorageDirectory().getAbsolutePath();LogEx.i("DownloadPathUtil sd_default", sd_default);// sd_default = /storage/emulated/0if (sd_default.endsWith("/")){sd_default = sd_default.substring(0, sd_default.length() - 1);}sd_default = ?/storage/emulated/0 if 分支不會走的
打印的line:
05-16 15:53:19.936 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:19 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?rootfs / rootfs ro,relatime 0 0
05-16 15:53:19.946 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:19 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?tmpfs /dev tmpfs rw,seclabel,nosuid,relatime,size=1248900k,nr_inodes=130627,mode=755 0 0
05-16 15:53:19.956 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:19 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?devpts /dev/pts devpts rw,seclabel,relatime,mode=600 0 0
05-16 15:53:19.956 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:19 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?none /dev/cpuctl cgroup rw,relatime,cpu 0 0
05-16 15:53:19.966 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:19 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?proc /proc proc rw,relatime 0 0
05-16 15:53:19.966 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:19 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?sysfs /sys sysfs rw,seclabel,relatime 0 0
05-16 15:53:19.976 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:19 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?selinuxfs /sys/fs/selinux selinuxfs rw,relatime 0 0
05-16 15:53:19.986 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:20 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?debugfs /sys/kernel/debug debugfs rw,relatime 0 0
05-16 15:53:19.996 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:20 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?none /acct cgroup rw,relatime,cpuacct 0 0
05-16 15:53:20.006 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:20 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?tmpfs /mnt/secure tmpfs rw,seclabel,relatime,size=1248900k,nr_inodes=130627,mode=700 0 0
05-16 15:53:20.016 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:20 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?tmpfs /mnt/asec tmpfs rw,seclabel,relatime,size=1248900k,nr_inodes=130627,mode=755,gid=1000 0 0
05-16 15:53:20.026 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:20 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?tmpfs /mnt/obb tmpfs rw,seclabel,relatime,size=1248900k,nr_inodes=130627,mode=755,gid=1000 0 0
05-16 15:53:20.036 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:20 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?/dev/block/platform/msm_sdcc.1/by-name/persdata /persdata/absolute ext4 rw,seclabel,nosuid,nodev,relatime,data=ordered 0 0
05-16 15:53:20.046 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:20 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?/dev/block/platform/msm_sdcc.1/by-name/system /system ext4 ro,seclabel,nosuid,nodev,relatime,data=ordered 0 0
05-16 15:53:20.056 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:20 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?/dev/block/platform/msm_sdcc.1/by-name/userdata /data ext4 rw,seclabel,nosuid,nodev,noatime,discard,journal_checksum,journal_async_commit,noauto_da_alloc,data=ordered 0 0
05-16 15:53:20.066 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:20 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?/dev/block/platform/msm_sdcc.1/by-name/cache /cache ext4 rw,seclabel,nosuid,nodev,noatime,discard,journal_checksum,journal_async_commit,noauto_da_alloc,data=ordered 0 0
05-16 15:53:20.076 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:20 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?/dev/block/platform/msm_sdcc.1/by-name/persist /persist ext4 rw,seclabel,nosuid,nodev,noatime,discard,journal_checksum,journal_async_commit,noauto_da_alloc,data=ordered 0 0
05-16 15:53:20.086 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:20 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?/dev/block/platform/msm_sdcc.1/by-name/efs /efs ext4 rw,seclabel,nosuid,nodev,noatime,discard,journal_checksum,journal_async_commit,noauto_da_alloc,data=ordered 0 0
05-16 15:53:20.096 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:20 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?/dev/block/platform/msm_sdcc.1/by-name/apnhlos /firmwarevfat ro,context=u:object_r:firmware:s0,relatime,uid=1000,gid=1000,fmask=0337,dmask=0227,codepage=cp437,iocharset=iso8859-1,shortname=lower,errors=remount-ro 0 0
05-16 15:53:20.106 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:20 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?/dev/block/platform/msm_sdcc.1/by-name/modem /firmware-modem vfat ro,context=u:object_r:firmware:s0,relatime,uid=1000,gid=1000,fmask=0337,dmask=0227,codepage=cp437,iocharset=iso8859-1,shortname=lower,errors=remount-ro 0 0
05-16 15:53:20.116 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:20 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?/data/media /mnt/shell/emulated sdcardfs rw,nosuid,nodev,relatime,uid=1023,gid=1023 0 0
05-16 15:53:20.126 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:20 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?/data/container /mnt/shell/container sdcardfs rw,nosuid,nodev,relatime,uid=1000,gid=1000 0 0
05-16 15:53:20.136 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:20 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?tmpfs /storage/emulated tmpfs rw,seclabel,nosuid,nodev,relatime,mode=050,gid=1028 0 0
05-16 15:53:20.146 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:20 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?/dev/block/vold/179:65 /storage/extSdCard vfat rw,dirsync,nosuid,nodev,noexec,noatime,nodiratime,uid=1000,gid=1023,fmask=0002,dmask=0002,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0
05-16 15:53:20.156 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:20 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?tmpfs /storage/extSdCard/.android_secure tmpfs ro,seclabel,relatime,size=0k,mode=000 0 0
05-16 15:53:20.166 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:20 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?/data/media /storage/emulated/0 sdcardfs rw,nosuid,nodev,relatime,uid=1023,gid=1023 0 0
05-16 15:53:20.176 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:20 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?/data/media /storage/emulated/0/Android/obb sdcardfs rw,nosuid,nodev,relatime,uid=1023,gid=1023 0 0
05-16 15:53:20.186 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:20 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?/data/media /storage/emulated/legacy sdcardfs rw,nosuid,nodev,relatime,uid=1023,gid=1023 0 0
05-16 15:53:20.196 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:53:20 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?/data/media /storage/emulated/legacy/Android/obb sdcardfs rw,nosuid,nodev,relatime,uid=1023,gid=1023 0 0
通過代碼看到 ?含有“secure” ?有? ? ? ??背景的兩行去掉,那么含有“extSdCard”也去掉了!!!
通過代碼看到 “asec” 有 ? ? ? ? 背景去掉!!!!
通過代碼看到 “mnt” 有?? ? ? ??背景!! 通過代碼看到 “fat” 有?? ? ? ??背景! 含有“fuse”沒有一行!! ?而且也不同時含有“mut”和“fat”!!
打印出來的sdcard_path:
05-16 15:54:46.886 I/DownloadPathUtil(25251): 2017年5月16日 PM3:54:46 ? ?F[DownloadPathUtil] ? ?L[497][getOuterSDPath] ? ? ?/storage/extSdCard
輸出日志關系:
05-16 15:54:46.866 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:54:46 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?/dev/block/vold/179:65 /storage/extSdCardvfat rw,dirsync,nosuid,nodev,noexec,noatime,nodiratime,uid=1000,gid=1023,fmask=0002,dmask=0002,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0
05-16 15:54:46.876 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:54:46 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?tmpfs /storage/extSdCard/.android_secure tmpfs ro,seclabel,relatime,size=0k,mode=000 0 0
05-16 15:54:46.886 I/DownloadPathUtil(25251): 2017年5月16日 PM3:54:46 ? ?F[DownloadPathUtil] ? ?L[497][getOuterSDPath] ? ? ?/storage/extSdCard
05-16 15:54:46.866 I/DownloadPathUtil line:(25251): 2017年5月16日 PM3:54:46 ? ?F[DownloadPathUtil] ? ?L[450][getOuterSDPath] ? ? ?/dev/block/vold/179:65 /storage/extSdCard?vfat rw,dirsync,nosuid,nodev,noexec,noatime,nodiratime,uid=1000,gid=1023,fmask=0002,dmask=0002,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0
這一行不含有 “secure” 分割之后,就會得到“/storage/extSdCard” ? 也就是上圖中? ? ? 含有fat和extSdCard 那一行!!!!!
到此日志分析結束。之所以使用“mnt”,“fuse”和“fat”分支是為了匹配市場上其他非主流手機。
獲取存儲空間大小:
/**** <p>* Description: 獲取手機外插SD 存儲容量 MB* <p>* @date 2017-4-18* @author * @param* @return*/public static long getOuterSDFree(){String sdcard_path = getOuterSDPath();if(TextUtils.isEmpty(sdcard_path) || TextUtils.isEmpty(sdcard_path.trim())){return 0;}StatFs stat = new StatFs(sdcard_path);long blockSize = stat.getBlockSize();long availableBlocks = stat.getAvailableBlocks();return availableBlocks * blockSize / 1024 / 1024;}
/***********************************************************************************************************************************************************************************/
問題來了:對于華為手機 打印line
05-18 15:47:29.930 D/DownloadPathUtil line:(13848): May 18, 2017 15:47:29 ? F[DownloadPathUtil] ? ?L[456][getOuterSDPath] ? ? ?tmpfs /dev tmpfs rw,seclabel,nosuid,relatime,size=1405708k,nr_inodes=351427,mode=755 0 0
05-18 15:47:29.931 D/DownloadPathUtil line:(13848): May 18, 2017 15:47:29 ? F[DownloadPathUtil] ? ?L[456][getOuterSDPath] ? ? ?devpts /dev/pts devpts rw,seclabel,relatime,mode=600 0 0
05-18 15:47:29.934 D/DownloadPathUtil line:(13848): May 18, 2017 15:47:29 ? F[DownloadPathUtil] ? ?L[456][getOuterSDPath] ? ? ?none /dev/cpuctl cgroup rw,relatime,ionice,cpu 0 0
05-18 15:47:29.936 D/DownloadPathUtil line:(13848): May 18, 2017 15:47:29 ? F[DownloadPathUtil] ? ?L[456][getOuterSDPath] ? ? ?none /dev/cpuset cgroup rw,relatime,cpuset,noprefix,release_agent=/sbin/cpuset_release_agent 0 0
05-18 15:47:29.938 D/DownloadPathUtil line:(13848): May 18, 2017 15:47:29 ? F[DownloadPathUtil] ? ?L[456][getOuterSDPath] ? ? ?none /dev/frz cgroup rw,relatime,freezer 0 0
05-18 15:47:29.939 D/DownloadPathUtil line:(13848): May 18, 2017 15:47:29 ? F[DownloadPathUtil] ? ?L[456][getOuterSDPath] ? ? ?proc /proc proc rw,relatime 0 0
05-18 15:47:29.942 D/DownloadPathUtil line:(13848): May 18, 2017 15:47:29 ? F[DownloadPathUtil] ? ?L[456][getOuterSDPath] ? ? ?sysfs /sys sysfs rw,seclabel,relatime 0 0
05-18 15:47:29.944 D/DownloadPathUtil line:(13848): May 18, 2017 15:47:29 ? F[DownloadPathUtil] ? ?L[456][getOuterSDPath] ? ? ?selinuxfs /sys/fs/selinux selinuxfs rw,relatime 0 0
05-18 15:47:29.945 D/DownloadPathUtil line:(13848): May 18, 2017 15:47:29 ? F[DownloadPathUtil] ? ?L[456][getOuterSDPath] ? ? ?none /sys/fs/cgroup tmpfs rw,seclabel,relatime,size=1405708k,nr_inodes=351427,mode=750,gid=1000 0 0
05-18 15:47:29.946 D/DownloadPathUtil line:(13848): May 18, 2017 15:47:29 ? F[DownloadPathUtil] ? ?L[456][getOuterSDPath] ? ? ?none /sys/fs/cgroup/pids cgroup rw,relatime,pids 0 0
05-18 15:47:29.947 D/DownloadPathUtil line:(13848): May 18, 2017 15:47:29 ? F[DownloadPathUtil] ? ?L[456][getOuterSDPath] ? ? ?pstore /sys/fs/pstore pstore rw,seclabel,relatime 0 0
05-18 15:47:29.948 D/DownloadPathUtil line:(13848): May 18, 2017 15:47:29 ? F[DownloadPathUtil] ? ?L[456][getOuterSDPath] ? ? ?/sys/kernel/debug /sys/kernel/debug debugfs rw,seclabel,relatime,gid=1000,mode=755 0 0
05-18 15:47:29.950 D/DownloadPathUtil line:(13848): May 18, 2017 15:47:29 ? F[DownloadPathUtil] ? ?L[456][getOuterSDPath] ? ? ?none /acct cgroup rw,relatime,cpuacct 0 0
05-18 15:47:29.951 D/DownloadPathUtil line:(13848): May 18, 2017 15:47:29 ? F[DownloadPathUtil] ? ?L[456][getOuterSDPath] ? ? ?tmpfs /mnt tmpfs rw,seclabel,relatime,size=1405708k,nr_inodes=351427,mode=755,gid=1000 0 0
05-18 15:47:29.952 D/DownloadPathUtil line:(13848): May 18, 2017 15:47:29 ? F[DownloadPathUtil] ? ?L[456][getOuterSDPath] ? ? ?/data/media /mnt/runtime/default/emulated sdcardfs rw,nosuid,nodev,relatime,uid=1023,gid=1023,derive=legacy,reserved=20MB 0 0
05-18 15:47:29.954 D/DownloadPathUtil line:(13848): May 18, 2017 15:47:29 ? F[DownloadPathUtil] ? ?L[456][getOuterSDPath] ? ? ?/data/media /mnt/runtime/read/emulated sdcardfs rw,nosuid,nodev,relatime,uid=1023,gid=1023,derive=legacy,reserved=20MB 0 0
05-18 15:47:29.955 D/DownloadPathUtil line:(13848): May 18, 2017 15:47:29 ? F[DownloadPathUtil] ? ?L[456][getOuterSDPath] ? ? ?/data/media /mnt/runtime/write/emulated sdcardfs rw,nosuid,nodev,relatime,uid=1023,gid=1023,derive=legacy,reserved=20MB 0 0
05-18 15:47:29.957 D/DownloadPathUtil line:(13848): May 18, 2017 15:47:29 ? F[DownloadPathUtil] ? ?L[456][getOuterSDPath] ? ? ?/dev/block/vold/public:179:145 /mnt/media_rw/3632-6364 vfat rw,dirsync,nosuid,nodev,noexec,relatime,uid=1023,gid=1023,fmask=0007,dmask=0007,allow_utime=0020,codepage=437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0
輸出的 路徑:?/mnt/media_rw/3632-6364 ? 使用去尋找路徑,這個是直接報錯的!!!
使用下面代碼:
/** * * <p> * Description: 通過反射獲取SD卡路徑:內置和外置(3.0以上) * <p> * @date 2017-4-18 * @author * @param * @return */ public static void getVolumePaths(Context context){String[] paths = null; Method mMethodGetPaths = null; try {Object storageManagerObj = context.getSystemService("storage"); if (storageManagerObj != null){mMethodGetPaths = storageManagerObj.getClass().getMethod("getVolumePaths"); if (mMethodGetPaths != null){paths = (String[]) mMethodGetPaths.invoke(storageManagerObj); }}}catch (Exception e){e.printStackTrace(); return; }if (paths != null && paths.length >= 2){String str = paths[1]; if(!TextUtils.isEmpty(str)){LogEx.i("DownloadPathUtil str=", str); File file = new File(str); if (file != null && file.isDirectory() && file.canExecute()){mExternalSDPath = str; //Android 4.4以上 // if(Build.VERSION.SDK_INT >= 19) // { // mExternalSDPath = str + "/Android/data/" + // context.getPackageName() + DownloadSettingVo.DOWNLOAD_PATH; // } // else // { // mExternalSDPath = str + DownloadSettingVo.DOWNLOAD_PATH; // } LogEx.i("DownloadPathUtil mExternalSDPath=", mExternalSDPath); }}}}
獲取的 路徑是:?L[563][getVolumePaths] ? ? ?/storage/3632-6364
所以只能采用下面這個方法獲取外插sd卡。
總結
以上是生活随笔為你收集整理的android 获得手机外插SD卡 存储路径和存储空间的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 说说Javaweb 服务器与Tomcat
- 下一篇: 遍地开花!比亚迪在淄博开汽车销售公司 注