获取外置卡、内置卡、USB路径的封装类

public class Dev_MountInfo
{
/*
* #######################
* ## Regular device mount
* ##
* ## Format: dev_mount <label> <mount_point> <part> <sysfs_path1...>
* ## label - Label for the volume
* ## mount_point - Where the volume will be mounted
* ## part - Partition # (1 based), or 'auto' for first usable partition.
* ## <sysfs_path> - List of sysfs paths to source devices
* ######################
*/
public final String HEAD = "dev_mount";
private final int DEV_INTERNAL = 0;
private final int DEV_EXTERNAL = 1;
private final int DEV_USB = 2;// 这个可能没有,上面两个一般都会有


private ArrayList<String> cache = new ArrayList<String>();


private static Dev_MountInfo dev;
private DevInfo info;


private final File VOLD_FSTAB = new File(Environment.getRootDirectory()
.getAbsoluteFile()
+ File.separator + "etc" + File.separator + "vold.fstab");


public static Dev_MountInfo getInstance()
{
if(null == dev)
dev = new Dev_MountInfo();
return dev;
}


private DevInfo getInfo(final int device)
{
try
{
initVoldFstabToCache();
} catch (IOException e)
{
e.printStackTrace();
}


info = new DevInfo();
if(device < cache.size())
{
String[] sinfo = cache.get(device).split(" |\t");// 这里只能跳过一个,多个的也得考虑啊
ArrayList<String> arr = new ArrayList<String>();// = new String[];
for (String s : sinfo)
{// int i=0; i<sinfo.length; i++){
if(!s.isEmpty())
{
arr.add(s);
}
}
if(arr.size() >= 5)
{
info.setLabel(arr.get(1));
info.setMount_point(arr.get(2));
info.setPart(arr.get(3));
info.setSysfs_path(arr.get(4));
}
// if(sinfo.length >= 5){
// info.setLabel(sinfo[1]);
// info.setMount_point(sinfo[2]);
// info.setPart(sinfo[3]);
// info.setSysfs_path(sinfo[4]);
// }
}
return info;
}


/**
* init the words into the cache array

* @throws IOException
*/
private void initVoldFstabToCache() throws IOException
{
cache.clear();
BufferedReader br = new BufferedReader(new FileReader(VOLD_FSTAB));
String tmp = null;
while ((tmp = br.readLine()) != null)
{
// the words startsWith "dev_mount" are the SD info
if(tmp.startsWith(HEAD))
{
cache.add(tmp);
// Log.v("xcl", tmp);
}
}
br.close();
cache.trimToSize();
}


public class DevInfo
{
private String label, mount_point, part;
private String sysfs_path;


public String getLabel()
{
return label;
}


private void setLabel(String label)
{
this.label = label;
}


public String getMount_point()
{
return mount_point;
}


private void setMount_point(String mount_point)
{
this.mount_point = mount_point;
}


public String getPart()
{
return part;
}


private void setPart(String part)
{
this.part = part;
}


public String getSysfs_path()
{
return sysfs_path;
}


private void setSysfs_path(String sysfs_path)
{
this.sysfs_path = sysfs_path;
}


}


public DevInfo getInternalInfo()
{
return getInfo(DEV_INTERNAL);
}


public DevInfo getExternalInfo()
{
return getInfo(DEV_EXTERNAL);
}


public DevInfo getUSBInfo()
{
return getInfo(DEV_USB);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值