0

With Android 6.0 and Above, not able to compile the below code.

It is not able to import android.os.storage.VolumeInfo

Needs to get volumeInfos from getVolumes() API.

Below is the code.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        StorageManager sm = ctx.getSystemService(StorageManager.class);
        List<VolumeInfo> volumeInfos = sm.getVolumes();
        for (VolumeInfo vol : volumeInfos) {
            if(vol.type==VolumeInfo.TYPE_PUBLIC
                    && (vol.state==VolumeInfo.STATE_MOUNTED || vol.state==VolumeInfo.STATE_MOUNTED_READ_ONLY)){
                String desc = sm.getBestVolumeDescription(vol);
                boolean isSdCard = desc.toLowerCase().contains("sd");
                list.add(new StorageInfo(vol.path, true, isSdCard, vol.fsUuid, vol.fsLabel, desc, isSdCard?0:usbCounter++));
                    }
             }

        return list;
    } 
tomerpacific
  • 4,704
  • 13
  • 34
  • 52

1 Answers1

0

VolumeInfo is no longer available, however you can use StorageVolume

Here is some hint to get the details about a storage volume

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            try {
                StorageManager mStorageManager =
                        (StorageManager) getSystemService(Context.STORAGE_SERVICE);

                Class<?> storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");

                if (mStorageManager != null) {
                    Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
                    Method getUuid = storageVolumeClazz.getMethod("getUuid");
                    Method getPath = storageVolumeClazz.getMethod("getPath");
                    Method getIsPrimary = storageVolumeClazz.getMethod("isPrimary");
                    Method getIsRemovable = storageVolumeClazz.getMethod("isRemovable");
                    Object result = getVolumeList.invoke(mStorageManager);

                    //Here you can do with the  more thing you need in the same way

                    if (result != null) {
                        //Iterate All Volumes
                        for (int i = 0; i < Array.getLength(result); i++) {
                            Object anStorageObject = Array.get(result, i);
                            String path = (String) getPath.invoke(anStorageObject);
                            String uuid = (String) getUuid.invoke(anStorageObject);
                            Boolean isPrimary = (Boolean) getIsPrimary.invoke(anStorageObject);
                            Boolean isRemovable = (Boolean) getIsRemovable.invoke(anStorageObject);

                        }
                    }


                }
            } catch (Exception e) {
                e.printStackTrace();

            }
        }
Jawad Zeb
  • 523
  • 4
  • 18