22

我对新的权限系统有疑问。我修改了我的代码以请求 WRITE_EXTERNAL_STORAGE 的许可。但是我的应用程序无法写入(或读取)SD卡,直到我重新启动应用程序(当然我在对话框中允许它)。之后它工作正常。

这不是一个新的应用程序,我已经在清单中声明了 WRITE_EXTERNAL_STORAGE,并且在早期的系统上它工作正常。

我正在使用 checkSelfPermission 和 requestPermissions 方法,当我向 sd 卡发出读取或写入请求时,我遇到了一个我没有权限的异常。

有人知道我错过了什么吗?还是我遇到了错误?

SDK版本是最新的(几个小时前更新),我正在模拟器上测试它。

4

1 回答 1

3

Callback用户允许WRITE_EXTERNAL_STORAGE来自RuntimePermission dialog.

首先你需要在你的和覆盖 方法中实现OnRequestPermissionsResultCallback Listener类来检查用户是允许还是拒绝权限ActivityCompatActivityvoid onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)

你可以这样做:

int REQUEST_STORAGE = 1;

private void checkExternalStoragePermissions() {
    if (hasStoragePermissionGranted()) {
        //You can do what whatever you want to do as permission is granted
    } else {
        requestExternalStoragePermission();
    }
}

public boolean hasStoragePermissionGranted(){
        return  ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
}

public void requestExternalStoragePermission() {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
        ActivityCompat.requestPermissions(MainActivity.this, {Manifest.permission.WRITE_EXTERNAL_STORAGE},
                REQUEST_STORAGE);
    }
}

onRequestPermissionsResult()当用户从运行时权限对话框允许或拒绝权限时,将调用以下方法。

您还可以处理用户已选中从不显示运行时权限对话框的情况。

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {

        if (requestCode == REQUEST_STORAGE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
               //User allow from permission dialog
               //You can do what whatever you want to do as permission is granted
            } else if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivtity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
               //User has deny from permission dialog
               Snackbar.make(mainLayout, getResources().getString("Please enable storage permission"),Snackbar.LENGTH_INDEFINITE)
                      .setAction("OK", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                ActivityCompat.requestPermissions(MainActivity.this, {Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_STORAGE);
                            }
                       })
                       .show();
            } else {
                // User has deny permission and checked never show permission dialog so you can redirect to Application settings page
                Snackbar.make(mainLayout, getResources().getString("Please enable permission from settings"),Snackbar.LENGTH_INDEFINITE)
                     .setAction("OK", new View.OnClickListener() {
                           @Override
                           public void onClick(View view) {
                                Intent intent = new Intent();                    intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                Uri uri = Uri.fromParts("package", MainActivity.this.getPackageName(), null);
                                intent.setData(uri);
                                startActivity(intent);
                            }
                       })
                       .show();
            }
        }
}

在这里,我曾经Snackbar向用户显示有关权限的相关消息,并且mainLayoutActivity's Main Layout.

希望它可以帮助你。

于 2015-11-20T04:22:17.793 回答