安卓6.0系统中存储权限错误

187
在Lollipop中,我的应用程序的下载功能运行良好,但当我升级到Marshmallow时,当我尝试从互联网下载到SD卡时,我的应用程序会崩溃并出现以下错误:
Neither user  nor current process has android.permission.WRITE_EXTERNAL_STORAGE

它抱怨这行代码:

DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);

我在应用程序外的清单中拥有权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

我清理并重新构建了项目,但仍然崩溃。


尝试一下这个,它可能会对你有所帮助:-https://dev59.com/J1wX5IYBdhLWcg3w-Thv#41221852 - Bipin Bharti
我已经准备了一个库,可以帮助轻松处理运行时权限。https://github.com/nabinbhandari/Android-Permissions - Nabin Bhandari
12个回答

0
经过大量的搜索,这段代码对我有用:
检查权限已有: 检查WRITE_EXTERNAL_STORAGE权限是否被允许?
if(isReadStorageAllowed()){
            //If permission is already having then showing the toast
            //Toast.makeText(SplashActivity.this,"You already have the permission",Toast.LENGTH_LONG).show();
            //Existing the method with return
            return;
        }else{
            requestStoragePermission();
        }



private boolean isReadStorageAllowed() {
    //Getting the permission status
    int result = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);

    //If permission is granted returning true
    if (result == PackageManager.PERMISSION_GRANTED)
        return true;

    //If permission is not granted returning false
    return false;
}

//Requesting permission
private void requestStoragePermission(){

    if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)){
        //If the user has denied the permission previously your code will come to this block
        //Here you can explain why you need this permission
        //Explain here why you need this permission
    }

    //And finally ask for the permission
    ActivityCompat.requestPermissions(this,new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_WRITE_STORAGE);
}

实现Override onRequestPermissionsResult方法以检查用户是否允许或拒绝

 @Override
 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    //Checking the request code of our request
    if(requestCode == REQUEST_WRITE_STORAGE){

        //If permission is granted
        if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){

            //Displaying a toast
            Toast.makeText(this,"Permission granted now you can read the storage",Toast.LENGTH_LONG).show();

        }else{
            //Displaying another toast if permission is not granted
            Toast.makeText(this,"Oops you just denied the permission",Toast.LENGTH_LONG).show();
        }
    }

0
以简单的方式,可以使用manifest.xml文件授予权限,但这只适用于API级别23 SDK版本6之前。从这里开始,如果我们想获取权限,就必须请求用户允许需要的权限。
只需将此代码添加到mainActivity.java中。
Override
            public void onClick(View view) {
                // Request the permission
                ActivityCompat.requestPermissions(MainActivity.this,
                        new String[]{Manifest.permission.CAMERA},
                        PERMISSION_REQUEST_CAMERA);

如果需要,可以将CAMERA替换或添加WRITE_EXTERNAL_STORAGE,并使用唯一代码。

                            new String[]{Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    101);

这是获取权限的简单代码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接