Android设备路径及容量的读取

原创
2015/05/24 14:15
阅读数 98

1.android设备上的所有外接设备路径总结:

/**
     * USB外接设备挂载路径
     */
    public static final String USB_STORAGE = "/mnt/usb_storage";

    /**
     * 外置内存卡路径
     */
    public static final String EXTERNAL_SD = "/mnt/external_sd";

    /**
     * 内置内存卡路径
     */
    public static final String INTERNAL_SD = Environment.getExternalStorageDirectory().getAbsolutePath();

或者可以采用代码读取方法获取所有存储设备的路径,读取方法为:
List<String> pathsList = new ArrayList<String>();
        
        StorageManager storageManager = (StorageManager) cxt.getSystemService(Context.STORAGE_SERVICE);
        try
        {
            Method method = StorageManager.class.getDeclaredMethod("getVolumePaths");
            method.setAccessible(true);
            Object result = method.invoke(storageManager);
            if (result != null && result instanceof String[])
            {
                String[] pathes = (String[]) result;
                StatFs statFs;
                for (String path : pathes)
                {
                    if (!TextUtils.isEmpty(path) && new File(path).exists())
                    {
                        statFs = new StatFs(path);
                        if (statFs.getBlockCount() * statFs.getBlockSize() != 0)
                        {
                            pathsList.add(path);
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            File externalFolder = Environment.getExternalStorageDirectory();
            if (externalFolder != null)
            {
                pathsList.add(externalFolder.getAbsolutePath());
            }
        }
        // }
        return pathsList.toArray(new String[pathsList.size()]);

2.容量获取方法

long availableSize = FileUtils.getAvailableSize(path);
        long usedSize,totalSize;
        if (path.startsWith(AppContants.USB_STORAGE))
        {
            usedSize = MeasurementUtils.measureDirectory(path);
            totalSize = availableSize + usedSize;
        }
        else
        {
            totalSize = FileUtils.getTotalSize(path);
            usedSize = totalSize - availableSize;
        }
其中,MeasurementUtils工具类为从android源码中的com.android.defcontainer包下获得的,如欲使用,
请从相应的包下获得并原样复制包名和类到你的应用下;
FileUtils工具类的方法为:
/**
     * 获取指定目录总空间
     * 
     * @param path
     * @return
     */
    @SuppressWarnings("deprecation")
    public static long getTotalSize(String path)
    {
        StatFs stat = new StatFs(path);
        long blockSize, totalBlocks;
        // 判断手机Android版本
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
        {
            blockSize = stat.getBlockSizeLong();
            totalBlocks = stat.getBlockCountLong();
            // availableBlocks = stat.getAvailableBlocksLong();
        }
        else
        {
            blockSize = stat.getBlockSize();
            totalBlocks = stat.getBlockCount();
            // availableBlocks = stat.getAvailableBlocks();
        }

        long totalSize = totalBlocks * blockSize;

        return totalSize;
    }

/**
     * 计算指定目录可用空间
     * 
     * @param path
     * @return
     */
    @SuppressWarnings("deprecation")
    public static long getAvailableSize(String path)
    {
        StatFs stat = new StatFs(path);
        long blockSize, availableBlocks;
        // 判断手机Android版本
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
        {
            blockSize = stat.getBlockSizeLong();
            // totalBlocks = stat.getBlockCountLong();
            availableBlocks = stat.getAvailableBlocksLong();
        }
        else
        {
            blockSize = stat.getBlockSize();
            // totalBlocks = stat.getBlockCount();
            availableBlocks = stat.getAvailableBlocks();
        }

        // long totalSize = totalBlocks * blockSize;
        long availSize = availableBlocks * blockSize;

        return availSize;
    }


展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部