Android Gets SD card storage space

Foreword

There needs to obtain external SD card storage space, but a method of using getExternalStorageDirectory out are stereotyped method Environment class, but this method Deprecated. It can only be used in other ways.

1.StorageManager

Gets an instance of StorageManager

storageManager = (StorageManager)mContext.getSystemService(mContext.STORAGE_SERVICE); 

2. Obtain memory card path

   private String[] getExtSDCardPath() { Log.d("SDRemount ","getExtSDCardPath"); try { Class<?>[] paramClasses = {}; Method getVolumePathsMethod = StorageManager.class.getMethod("getVolumePaths", paramClasses); getVolumePathsMethod.setAccessible(true); Object[] params = {}; Object invoke = getVolumePathsMethod.invoke(storageManager, params); return (String[])invoke; } catch (NoSuchMethodException e1) { e1.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return null; } 

3. Check mount

SD card mounted in the return path, well-tested, step on the path to acquiring all to mount. Wherein the first path: / storage / emulated / 0, which is stored within itself. When the number of the return path is greater than 1, there is an external memory.

    public String[] sdCardRemounted() { Log.d("SDRemount ","sdCardRemounted checked"); String[] data = getExtSDCardPath(); List<String> list =new ArrayList<>(); if (data.length > 0){ for(String path : data){ if(checkMounted(mContext,path)){ list.add(path); } } } int count = list.size(); String[] result =new String[count]; /*for (int i = 0;i < count; i++) { result[i]=list.get(i); }*/ return list.toArray(result); } 

Check the Mount:

    private boolean checkMounted(Context context, String mountPoint) { if (mountPoint == null) { Log.d("SDRemount ","mountPoint == null"); return false; } //StorageManager storageManager = (StorageManager) context //.getSystemService(Context.STORAGE_SERVICE); try { Method getVolumeState = storageManager.getClass().getMethod( "getVolumeState", String.class); String state = (String) getVolumeState.invoke(storageManager, mountPoint); //Log.d("SDRemount ","SDCard path " + mountPoint +" and state " +state); StatFs statFs = new StatFs(mountPoint); long blockSize=statFs.getBlockSizeLong(); long availableSize=statFs.getAvailableBlocksLong(); long totalSize=statFs.getBlockCountLong(); Log.d("SDRemount ","SDCard path " + mountPoint +" and state " +state + " total size "+ totalSize*blockSize + " available size " + availableSize*blockSize); return Environment.MEDIA_MOUNTED.equals(state); } catch (Exception e) { e.printStackTrace(); } return false; } 

4. Get the path storage

4.1 All space

    public long getTotalSize(String path){ StatFs statFs = new StatFs(path); long blockSize=statFs.getBlockSizeLong(); //long availableSize=statFs.getAvailableBlocksLong(); long totalSize=statFs.getBlockCountLong(); long total = blockSize * totalSize; Log.d("SDRemount ","SDCard path " + path +" total size "+ total); return total; } 

4.2 space available

    public long getAvailableSize(String path){ StatFs statFs = new StatFs(path); long blockSize=statFs.getBlockSizeLong(); long availableSize=statFs.getAvailableBlocksLong(); //long totalSize=statFs.getBlockCountLong(); long total = blockSize * availableSize; Log.d("SDRemount ","SDCard path " + path +" total size "+ total); return total; }

Guess you like

Origin www.cnblogs.com/llstart-new0201/p/11810110.html