sd卡路径

  public String externalSDCardPath() {
        try {
            StorageManager storageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
            List<StorageVolume> storageVolumes = storageManager.getStorageVolumes();
            Class<?> volumeClass = Class.forName("android.os.storage.StorageVolume");
            Method getPath = volumeClass.getDeclaredMethod("getPath");
            Method isRemovable = volumeClass.getDeclaredMethod("isRemovable");
            getPath.setAccessible(true);
            isRemovable.setAccessible(true);
            for (int i = 0; i < storageVolumes.size(); i++) {
                StorageVolume storageVolume = storageVolumes.get(i);
                String mPath = (String) getPath.invoke(storageVolume);
                Boolean isRemove = (Boolean) isRemovable.invoke(storageVolume);
                Log.d(TAG, "Path is === " + mPath + "isRemoveble == " + isRemove);
            }
        }catch (Exception e){
            Log.d(TAG,"error == "+e.getMessage());
        }
        return "";
    }

    private String getSDCardPath() throws Exception
    {
        StorageManager sm = (StorageManager) getSystemService(Context.STORAGE_SERVICE);

        Class<?> storageVolumeClass = Class.forName("android.os.storage.StorageVolume");
        Method getVolumeList = sm.getClass().getMethod("getVolumeList");
        Method getPath = storageVolumeClass.getMethod("getPath");
        Method isRemovable = storageVolumeClass.getMethod("isRemovable");
        Object result = getVolumeList.invoke(sm);
        for(int i = 0; i < Array.getLength(result); ++i)
        {
            Object element = Array.get(result, i);
            boolean removable = (Boolean) isRemovable.invoke(element);
            if(removable)
            {
                return (String) getPath.invoke(element);
            }
        }
        throw new RuntimeException("no suitable SD path found");
    }




    private StorageManager mStorageManager;
    private StorageVolume[] mStorageVolumes;
    private File mSdcard;
    private File mEmmc;
    private File mUsbStorage;

    private void updateStoragePaths() {
        if (mStorageManager == null) {
            mStorageManager = (StorageManager) getApplicationContext().getSystemService(Context.STORAGE_SERVICE);
            try {
                Method method = mStorageManager.getClass().getMethod("getVolumeList");
                mStorageVolumes = (StorageVolume[]) method.invoke(mStorageManager);
                for (int i = 0; i < mStorageVolumes.length; i++) {
                    Method method2 = mStorageVolumes[i].getClass().getMethod("getPathFile");
                    File storage = (File) method2.invoke(mStorageVolumes[i]);
                    switch (i) {
                        case 0:
                            mEmmc = storage;
                            break;
                        case 1:
                            mSdcard = storage;
                            long totalSpace = mSdcard.getTotalSpace();
                            long freeSpace = mSdcard.getFreeSpace();
                            long usableSpace = mSdcard.getUsableSpace();
                            String totalSpaceStr = Formatter.formatFileSize(this, totalSpace);
                            String freeSpaceStr = Formatter.formatFileSize(this, freeSpace);
                            String usableSpaceStr = Formatter.formatFileSize(this, usableSpace);
                            Log.d(TAG,"totalSpaceStr == "+totalSpaceStr);
                            Log.d(TAG,"freeSpaceStr == "+freeSpaceStr);
                            Log.d(TAG,"usableSpaceStr == "+usableSpaceStr);
                            break;
                        case 2:
                            mUsbStorage = storage;
                            break;
                    }
                }

            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值