登录后台

页面导航

本文编写于 218 天前,最后修改于 218 天前,其中某些信息可能已经过时。

android

申请动态申请权限还是不行就需要继续在AndroidManifest.xml加入

<application android:requestLegacyExternalStorage = "true" </application>

如果不加入的话只能在getExternalCacheDir()创建

动态申请权限:

String[] permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};
List<String> mPermissionList = new ArrayList<>();

// private ImageView welcomeImg = null;
private static final int PERMISSION_REQUEST = 1;
// 检查权限

private void checkPermission() {
    mPermissionList.clear();

    //判断哪些权限未授予
    for (int i = 0; i < permissions.length; i++) {
        if (ContextCompat.checkSelfPermission(this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
            mPermissionList.add(permissions[i]);
        }
    }
    /**
     * 判断是否为空
     */
    if (mPermissionList.isEmpty()) {//未授予的权限为空,表示都授予了

    } else {//请求权限方法
        String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);//将List转为数组
        ActivityCompat.requestPermissions(MainActivity.this, permissions, PERMISSION_REQUEST);
    }
}

/**
 * 响应授权
 * 这里不管用户是否拒绝,都进入首页,不再重复申请权限
 */
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case PERMISSION_REQUEST:

            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            break;
    }
}