How to get actual size of mounted SD card in Android?

前端 未结 2 555
庸人自扰
庸人自扰 2021-01-15 17:21

I\'m trying to find a way to find the total size and free space of a mounted SD card on a phone, from my research on SOF and on the Android devs site I was able to find the

2条回答
  •  渐次进展
    2021-01-15 17:49

    public String[] getSize() throws IOException {
        String memory="";
        Process p = Runtime.getRuntime().exec("df /mnt/sdcard");
        InputStream is =p.getInputStream();
        int by=-1;
        while((by=is.read())!=-1) {
            memory+=new String(new byte[]{(byte)by});
        }
        for (String df:memory.split("/n")) {
            if(df.startsWith("/mnt/sdcard")) {
                String[] par = df.split(" ");
                List pp=new ArrayList();
                for(String pa:par) {
                    if(!pa.isEmpty()) {
                        pp.add(pa);
                    }
                }
                return pp.toArray(new String[pp.size()]);
    
            }
        }
        return null;
    }
    

    getSize()[0] is /mnt/sdcard. getSize()[1] is size of sd (example 12.0G), getSize()[2] is used, [3] is free, [4] is blksize

    Or:

    new File("/sdcard/").getFreeSpace() - bytes of free in long
    new File("/sdcard/").getTotalSpace() - size of sd
    

提交回复
热议问题