• 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;
    // }
    // }
    }
    }
  • 相关阅读:
    Solr多核的配置
    Solr开发参考文档(转)
    Solr使用solr4J操作索引库
    Solr和IK分词器的整合
    Solr与mmseg4J的整合
    Lucene实例教程
    Lucene简介
    PHP实现大文件的上传设置
    图片上传预览功能实现
    Logstash 安装和使用
  • 原文地址:https://www.cnblogs.com/zyb2016/p/13561108.html
Copyright © 2020-2023  润新知