获取手机内存卡的路径(内置或外置)

 现在市面上的手机大多有内置sd卡,而用户有的会加个外置sd卡,有的则只使用内置的sd卡。那么我们如果要获取sd卡(内置或外置)的数据,该怎么做?


直接贴代码(代码是从网上的找的,自己修改了一下)


/**
     * 遍历 "system/etc/vold.fstab” 文件,获取全部的Android的挂载点信息
     *
     * @return
     */
    private static ArrayList<String> getDevMountList() {
        String[] toSearch = readSDFile("/etc/vold.fstab").split( " ");
        ArrayList<String> out = new ArrayList<String>();
        for (int i = 0; i < toSearch.length; i++) {
            if (toSearch[i].contains("dev_mount" )) {
                if (new File(toSearch[i + 2]).exists()) {
                    out.add(toSearch[i + 2]);
                }
            }
        }
        return out;
    }
   
    /**
     * 获取扩展SD卡存储目录
     *
     * 如果有外接的SD卡,并且已挂载,则返回这个外置SD卡目录
     * 否则:返回内置SD卡目录
     *
     * @return
     */
    public static String getExternalSdCardPath() {
 
        if (isMounted()) {
            File sdCardFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
            return sdCardFile.getAbsolutePath();
        }
 
        String path = null;
 
        File sdCardFile = null;
 
        ArrayList<String> devMountList = getDevMountList();
 
        for (String devMount : devMountList) {
            File file = new File(devMount);
 
            if (file.isDirectory() && file.canWrite()) {
                path = file.getAbsolutePath();
 
                String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmmss" ).format(new Dat e());
                File testWritable = new File(path, "test_" + timeStamp);
 
                if (testWritable.mkdirs()) {
                    testWritable.delete();
                } else {
                    path = null;
                }
            }
        }
 
        if (path != null) {
            sdCardFile = new File(path);
            return sdCardFile.getAbsolutePath();
        }
 
        return null ;
    }

    /**
        *
        * 读取SD卡中文本文件
        *
        *
        *
        * @param fileName
        *
        * @return
        */

        public static String readSDFile(String fileName) {

              StringBuffer sb = new StringBuffer();

              File file = new File(fileName);

               try {

                     FileInputStream fis = new FileInputStream(file);

                      int c;

                      while ((c = fis.read()) != -1) {

                           sb.append(( char) c);

                     }

                     fis.close();

              } catch (FileNotFoundException e) {

                     e.printStackTrace();

              } catch (IOException e) {

                     e.printStackTrace();

              }

               return sb.toString();

       }
       
        public static boolean isMounted() {
               if( Environment.getExternalStorageState().equals( "mounted"))
                      return true ;
               else
                      return false ;
       }



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值