Android获取存储信息

5.1和6.0以上获取SD卡和USB(OTG)存储大小:

要获取存储设备的存储大小,先需要获取设备的路径,我这里的设备SD卡都只有一张,固定为/mnt/external_sd,USB口可能有多个,网上说的一般是/mnt/usb_storage,但我找个几个5.1的平板都获取不到,最后通过adb shell df查看到了usb的实际路径并打印出所有路径,重新写了份获取USB路径的流程,整理如下:https://blog.csdn.net/l471979773/article/details/84890492

public class StorageUtil {

    Context context;
    private  String usb_dir = null;
    private  String sdcard_dir = null;
    private  List<String> sdcard_list = null;
    private  List<String> usb_list = null;
    
    String TAG ="StorageUtil" ;
    
    public StorageUtil(Context context) {
        this.context = context;
        sdcard_list = new ArrayList<String>();
        usb_list = new ArrayList<String>();
        initStoragePath();
    }
    
    //每次获取清除之前的数据,如果需要接收广播实时获取,重新调用此方法
    private void initStoragePath() {
        sdcard_list.clear();
        usb_list.clear();
        StorageManager mStorageManager = (StorageManager) context
                .getSystemService(Context.STORAGE_SERVICE);
        if (Build.VERSION.SDK_INT < 23) {
            StorageVolume[] storageVolumes = mStorageManager.getVolumeList();
            for (StorageVolume volume : storageVolumes) {

                String externalVolumeState = mStorageManager.getVolumeState(
                        volume.getPath()).toString();
                if (!externalVolumeState.equals(Environment.MEDIA_MOUNTED)
                        && !externalVolumeState
                                .equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
                    continue;
                }
                if (!volume.isEmulated()) {
                    if (volume.getPath().equals("/mnt/external_sd")) {//这里是为了和USB形成统一,若此路径不行,可通过adb shell df查看新增设备
                        sdcard_dir = volume.getPath();
                        sdcard_list.add(sdcard_dir);
                    } else if (volume.getPath().contains("/mnt/usb_storage")) {//USB存储目录,不一定是真正的USB路径
                        usb_dir = volume.getPath();
                        File[] files = new File(usb_dir).listFiles();//adb shell df 查看USB实际路径,我这实际路径在/mnt/usb_storage里面
                        Log.d(TAG, "get file " +  usb_dir );     
                        for (File file : files) {
                            usb_dir = file.toString();
                            Log.d(TAG, "usb_dir = " +  usb_dir );
                            usb_list.add(usb_dir);
                        }

                    }

                }

            }
            return;
        }else {//6.0到8.1都有这个方法,可以直接用
            final List<VolumeInfo> volumes = mStorageManager.getVolumes();
            Collections.sort(volumes, VolumeInfo.getDescriptionComparator());

            for (VolumeInfo vol : volumes) {
                if (vol.getType() == VolumeInfo.TYPE_PUBLIC) {
                    Log.d(TAG, "Volume path:" + vol.getPath());
                    DiskInfo disk = vol.getDisk();
                    if (disk != null) {
                        if (disk.isUsb()) {
                            // sdcard dir
                            StorageVolume sv = vol.buildStorageVolume(context,
                                    context.getUserId(), false);
                            usb_dir = sv.getPath();
                            usb_list.add(usb_dir);
                        } else if (disk.isSd()) {
                            StorageVolume sv = vol.buildStorageVolume(context,
                                    context.getUserId(), false);
                            sdcard_dir = sv.getPath();
                            sdcard_list.add(sdcard_dir);
                        }
                    }
                }
            }
        }

    }
    
    //所有SD卡路径
    public List<String> getSdcard_list() {
        return sdcard_list;
    }

    //所有usb路径
    public List<String> getUsb_list() {
        return usb_list;
    }

    //SD卡的路径
    public String getSdcard_dir() {
        return sdcard_dir;
    }
    
}

知道路径了,存储大小也好获取了,网上有特别多,usb和sd卡获取大小的都一样,我没怎么整理,就把代码贴在这:

        File pathFile = new File(USB_PATH);//单独取出usb的每个路径
        if (!pathFile.exists()) {//文件不存在则返回
            return;
        }
        try {
            stat = new StatFs(pathFile.getPath());
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        long total = stat.getTotalBytes();//总大小
        long avi = stat.getAvailableBytes();//可用大小
        String totalSize = Formatter.formatFileSize(this, total);
        String aviSize = Formatter.formatFileSize(this, avi);

PS:如果需要监听插拔设备,我这拔卡的广播会走三次,注意清除数据。

转载请写明出处,若有问题可留言。https://blog.csdn.net/l471979773/article/details/84890492

猜你喜欢

转载自blog.csdn.net/l471979773/article/details/84890492