zoukankan      html  css  js  c++  java
  • androidstudio安卓获取外部sd卡存储

    private static String getStoragePath(Context mContext, boolean is_removale) {

    StorageManager mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
    Class<?> storageVolumeClazz = null;
    try {
    storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
    Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
    Method getPath = storageVolumeClazz.getMethod("getPath");
    Method isRemovable = storageVolumeClazz.getMethod("isRemovable");
    Object result = getVolumeList.invoke(mStorageManager);
    final int length = Array.getLength(result);
    for (int i = 0; i < length; i++) {
    Object storageVolumeElement = Array.get(result, i);
    String path = null;
    try {
    path = (String) getPath.invoke(storageVolumeElement);
    } catch (InvocationTargetException e) {
    e.printStackTrace();
    }
    boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement);
    if (is_removale == removable) {
    return path;
    }
    }
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    } catch (InvocationTargetException e) {
    e.printStackTrace();
    } catch (NoSuchMethodException e) {
    e.printStackTrace();
    } catch (IllegalAccessException e) {
    e.printStackTrace();
    }
    return null;
    }



    /**
    * 6.0使用此方法获取外置SD卡路径,尝试过反射{@link StorageManager#getVolumeList}
    * 但StorageVolume非Public API 编译不通过(7.0改为公开API),故使用UserEnvironment
    * 的内部方法getExternalDirs获取所有的路径,通过{@link Environment#isExternalStorageRemovable(File)}
    * 判断若removable则为外部存储
    */
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    //@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private static String getPhysicalExternalFilePathAboveM(){
    try {
    //===========================获取UserEnvironment================
    Class<?> userEnvironment = Class.forName("android.os.Environment$UserEnvironment");
    Method getExternalDirs =userEnvironment.getDeclaredMethod("getExternalDirs");
    getExternalDirs.setAccessible(true);
    //========获取构造UserEnvironment的必要参数UserId================
    Class<?> userHandle = Class.forName("android.os.UserHandle");
    Method myUserId = userHandle.getDeclaredMethod("myUserId");
    myUserId.setAccessible(true);
    int mUserId = (int) myUserId.invoke(UserHandle.class);
    Constructor<?> declaredConstructor = userEnvironment.getDeclaredConstructor(Integer.TYPE);
    // 得到UserEnvironment instance
    Object instance = declaredConstructor.newInstance(mUserId);
    File[] files = (File[]) getExternalDirs.invoke(instance);
    for (int i = 0; i < files.length; i++) {
    if (Environment.isExternalStorageRemovable(files[i])){
    return files[i].getPath();
    }
    }
    } catch (Exception e) {

    }
    return "";
    }

    使用sd卡权限前请求权限
        public void getPermissions(){
    if (Build.VERSION.SDK_INT >= 23) {
    int REQUEST_CODE_PERMISSION_STORAGE = 100;
    String[] permissions = {
    Manifest.permission.READ_EXTERNAL_STORAGE,
    Manifest.permission.WRITE_EXTERNAL_STORAGE//, "android.permission.WRITE_MEDIA_STORAGE"
    };

    this.requestPermissions(permissions,REQUEST_CODE_PERMISSION_STORAGE);

    // for (String str : permissions) {
    // if (this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
    // this.requestPermissions(permissions, REQUEST_CODE_PERMISSION_STORAGE);
    // return;
    // }
    // }
    }
    }
  • 相关阅读:
    css---box-sizing
    float与inline-block的一些应用场景的区别
    一些html元素的最原始状态
    css之深入理解overflow
    css中的锚点
    新增UI样式
    zh-CN、zh-Hans区别
    SourceTree 3.3.6安装跳过注册安装
    Windows sever 由于管理员设置的策略,该磁盘处于脱机状态的解决方法。
    CentOS7.x安装VNC实录
  • 原文地址:https://www.cnblogs.com/zyb2016/p/13561108.html
Copyright © 2011-2022 走看看