Android获取机身存储、内置SD卡与外置TF卡路径

获取机身存储路径(可以通过openFileInput,openFileOutput进行操作)

String path=Environment.getDataDirectory().getAbsolutePath();返回/data

获取内置SD卡路径:

public String getStorageDir(){
    	 if(!(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))){
    		 return "";
    	 }
    	 File dirFile=Environment.getExternalStorageDirectory();
    	 Log.d(TAG, dirFile.getAbsolutePath());
    	 return dirFile.getAbsolutePath();
    }

返回/storage/emulated/o

获取外置TF卡路径:

思路:通过linux中的mount命令。

public String getTFDir(){
		String path="";
		try {
			InputStream ins=Runtime.getRuntime().exec("mount").getInputStream();
			BufferedReader reader=new BufferedReader(new InputStreamReader(ins));
			String line="";
			while((line=reader.readLine())!=null){
				if(line.contains("sdcard")){
					if(line.contains("vfat")||line.contains("fuse")){
						String split[]=line.split(" ");
						path=split[1];
						Log.d(TAG,path);

					}
				}
			}
			reader.close();
			ins.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			
		}
		return path;
	}
返回/storage/sdcard1这就是我们想要的路径。

获取可用空间

public static long getAvailableSize(String path){
    try{
        File base = new File(path);
    StatFs stat = new StatFs(base.getPath());
    long nAvailableCount = stat.getBlockSize() * ((long) stat.getAvailableBlocks());
    return nAvailableCount;
    }catch(Exception e){
    e.printStackTrace();
    }
    return 0;
    }
返回bytes单位的大小。





  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值