Android SDK22以下 读写手机存储权限获取失败问题处理方法

针对小米手机sdk22以下sdk获取不到文件 存储权限问题。
 

/****************** Android SDK22 读写手机存储权限获取问题处理方法 **************************/
    @NeedsPermission({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void externalStorage() {
        // 是否需要重启
        if (KDApplication.getInstance().isReStartApp()){
            isReStartAppDialog(this);
        }else {
            chouseHeadImg();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
       UserInfoActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }

    @OnShowRationale({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void externalStorageShow(final PermissionRequest request) {
    }

    @OnPermissionDenied({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void externalStorageDenied() {
        openPermissionDialog(UserInfoActivity.this,"读写手机存储");
    }

    @OnNeverAskAgain({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void externalStorageNeverAskAgain() {
    }

    private AlertDialog.Builder dialogBuilder;
    private AlertDialog dialog;
    /**
     * 打开手机权限设置对话框
     *
     * @param permission
     */
    public void openPermissionDialog(final Activity activity, String permission) {
        dialogBuilder = new AlertDialog.Builder(activity);
        dialogBuilder.setTitle("手机权限申请");
        dialogBuilder.setMessage("\t\t使用该功能需要开启 <" + permission + "权限> 请点击 [前往设置页面] 进行设置,并重启APP");
        dialogBuilder.setPositiveButton("前往设置页面", (dialog1, which) -> {
            // 前往设置页面
            activity.startActivity(PermissionsPageManager.getSettingIntent(
                    activity));
            dialog.dismiss();
            KDApplication.getInstance().setReStartApp(true);
        } );
        dialogBuilder.setNegativeButton("取消", (dialog1, which) -> {
            dialog.dismiss();
        } );
        dialog=dialogBuilder.show();
    }

    /**
     * 是否需要重启对话框
     */
    public void isReStartAppDialog(final Activity activity) {
        dialogBuilder = new AlertDialog.Builder(activity);
        dialogBuilder.setTitle("是否需要重启");
        dialogBuilder.setMessage("\t\t如果修改了APP权限设置,需要重启APP才可正常使用,是否重启App?");
        dialogBuilder.setPositiveButton("重启App", (dialog1, which) -> {
            reStartApp();
            KDApplication.getInstance().setReStartApp(false);
        } );
        dialogBuilder.setNegativeButton("取消", (dialog1, which) -> {
            dialog.dismiss();
        } );
        dialog=dialogBuilder.show();
    }

    /**
     * 重启App
     */
    public void reStartApp()
    {
        Intent intent = new Intent(UserInfoActivity.this, SplashActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        android.os.Process.killProcess(android.os.Process.myPid());
    }
    /****************** Android SDK22 读写手机存储权限获取问题处理方法 **************************/

使用了PermissionsDispatcher和Permissions4m核心代码。
PermissionsDispatcher github 地址:https://github.com/permissions-dispatcher/PermissionsDispatcher
Permissions4m github 地址:https://github.com/jokermonn/permissions4m
PermissionsPageManager github 地址:https://github.com/HHaobin/PermissionManager

小米手机图片调用剪裁图片失败可参考:https://blog.csdn.net/qq_33731155/article/details/86595824

你可能感兴趣的:(Android SDK22以下 读写手机存储权限获取失败问题处理方法)