简体   繁体   English

Android Studio - 不询问 Write_External_Storage

[英]Android Studio - not asking Write_External_Storage

在此处输入图片说明

在此处输入图片说明

How I'm requesting我如何请求

 public void requestPermissionToWriteToFile() {

 if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)){
            new AlertDialog.Builder(this)
                    .setTitle("Permission needed")
                    .setMessage("This permission is needed for camera use and internal file save")
                    .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                                    PERMISSION_REQUEST_WRITE_FILES);
                        }
                    })
                    .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).create().show();
    }
    else
    {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        PERMISSION_REQUEST_WRITE_FILES);
    }
}

How I'm Checking我如何检查

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if(requestCode == PERMISSION_REQUEST_WRITE_FILES){
        if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
        {Toast.makeText(this, "PERMISSION_GRANTED", Toast.LENGTH_SHORT).show();}
        else
        {Toast.makeText(this, "PERMISSION_DENIED", Toast.LENGTH_SHORT).show();}
    }
}

As soon as the app boots up, the accesses is never granted and it just stays that way, when I call on the above method the dialog for permission wont come up (the permission isn't being asked).一旦应用程序启动,访问权限就不会被授予,它只是保持这种状态,当我调用上述方法时,权限对话框不会出现(不要求权限)。 Even when I go to settings and change it from there it still won't grant me permission to WRITE_EXTERNAL_STORAGE.即使我转到设置并从那里更改它,它仍然不会授予我对 WRITE_EXTERNAL_STORAGE 的权限。 the above code is in my main class, the permission is in Manifest, what else is needed to fix this?上面的代码在我的主类中,权限在清单中,还需要什么来解决这个问题?

  • there was another thread like this but that didn't solve my problem jsyk.还有另一个像这样的线程,但这并没有解决我的问题 jsyk。

Before trying the following code, add the missing G in STORAGE.在尝试以下代码之前,在 STORAGE 中添加缺少的 G。

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

Then call this when your button is clicked:然后在单击按钮时调用它:

if (androidx.core.content.ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE) == android.content.pm.PackageManager.PERMISSION_DENIED || androidx.core.content.ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == android.content.pm.PackageManager.PERMISSION_DENIED) {

androidx.core.app.ActivityCompat.requestPermissions(MainActivity.this, new String[] {android.Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1001);
} else {
//permission already granted
}

Also, add read external storage permission to avoid any issues:此外,添加读取外部存储权限以避免任何问题:

<uses-permission    android:name="android.permission.READ_EXTERNAL_STORAGE"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM