Android权限已授予,但仍被拒绝访问

4

我的项目中有三个不同的活动。第一个活动包含所有权限请求。应用程序已被授予所有权限,但仍无法在SD卡上写入文件。如果我关闭应用程序并再次启动,那么我就可以在SD卡上写入文件。有没有办法更新多个活动的权限。

public class PermissionActivity extends AppCompatActivity {
public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;
private static int TIME_OUT = 2000;
private String TAG = "tag";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_permission);

    Locale locale = new Locale("en");
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    this.getResources().updateConfiguration(config, null);

    if(checkAndRequestPermissions()) {
        // carry on the normal flow, as the case of  permissions  granted.
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // This method will be executed once the timer is over
                // Start your app main activity

                Intent i = new Intent(PermissionActivity.this, MainActivity.class);
                startActivity(i);

                // close this activity
                finish();
            }
        }, TIME_OUT);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    Log.d(TAG, "Permission callback called-------");
    switch (requestCode) {
        case REQUEST_ID_MULTIPLE_PERMISSIONS: {

            Map<String, Integer> perms = new HashMap<>();
            // Initialize the map with both permissions
            perms.put(android.Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
            perms.put(android.Manifest.permission.WRITE_CALENDAR, PackageManager.PERMISSION_GRANTED);
            perms.put(android.Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
            perms.put(android.Manifest.permission.ACCESS_COARSE_LOCATION, PackageManager.PERMISSION_GRANTED);
            // Fill with actual results from user
            if (grantResults.length > 0) {
                for (int i = 0; i < permissions.length; i++)
                    perms.put(permissions[i], grantResults[i]);
                // Check for both permissions
                if (perms.get(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
                        && perms.get(android.Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED
                        && perms.get(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                        && perms.get(android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                    Log.d(TAG, "all permission granted");

                    // process the normal flow
                    Intent i = new Intent(PermissionActivity.this, MainActivity.class);
                    startActivity(i);
                    finish();
                    //else any one or both the permissions are not granted
                } else {
                    Log.d(G.mLogTag, "Some permissions are not granted ask again ");
                    if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                            || ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_CALENDAR)
                            || ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
                            || ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION)) {
                        showDialogOK("Service Permissions are required for this app",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        switch (which) {
                                            case DialogInterface.BUTTON_POSITIVE:
                                                checkAndRequestPermissions();
                                                break;
                                            case DialogInterface.BUTTON_NEGATIVE:
                                                // proceed with logic by disabling the related features or quit the app.
                                                finish();
                                                break;
                                        }
                                    }
                                });
                    }
                    else {
                        explain("You need to give some mandatory permissions to continue. Do you want to go to app settings?");
                    }
                }
            }
        }
    }
}

private boolean checkAndRequestPermissions(){
    int writeStoragePermission = ContextCompat.checkSelfPermission(PermissionActivity.this,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
    int writeCalenderPermission = ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.WRITE_CALENDAR);
    int fineLocationPermission = ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.ACCESS_FINE_LOCATION);
    int coarseLocationPermission = ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.ACCESS_COARSE_LOCATION);

    List<String> listPermissionsNeeded = new ArrayList<>();
    if (writeStoragePermission != PackageManager.PERMISSION_GRANTED)
        listPermissionsNeeded.add(android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (writeCalenderPermission != PackageManager.PERMISSION_GRANTED)
        listPermissionsNeeded.add(android.Manifest.permission.WRITE_CALENDAR);
    if (fineLocationPermission != PackageManager.PERMISSION_GRANTED)
        listPermissionsNeeded.add(android.Manifest.permission.ACCESS_FINE_LOCATION);
    if (coarseLocationPermission != PackageManager.PERMISSION_GRANTED)
        listPermissionsNeeded.add(android.Manifest.permission.ACCESS_COARSE_LOCATION);
    if (!listPermissionsNeeded.isEmpty()){
        ActivityCompat.requestPermissions(this, listPermissionsNeeded
                        .toArray(new String[listPermissionsNeeded.size()]),
                REQUEST_ID_MULTIPLE_PERMISSIONS);
        return false;
    }
    return true;
}

private void showDialogOK(String message, DialogInterface.OnClickListener okListener) {
    new AlertDialog.Builder(this)
            .setMessage(message)
            .setPositiveButton("OK", okListener)
            .setNegativeButton("Cancel", okListener)
            .create()
            .show();
}
private void explain(String msg){
    final android.support.v7.app.AlertDialog.Builder dialog = new android.support.v7.app.AlertDialog.Builder(this);
    dialog.setMessage(msg)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                    //  permissionsclass.requestPermission(type,code);
                    startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
                            Uri.parse("package:fi.maxitors.mydiscgolfapp")));
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                    finish();
                }
            });
    dialog.show();
}

以上是我的权限请求活动。对于糟糕的英语,我感到抱歉。


一旦用户批准权限,整个应用程序即被授予权限。如果您仍然无法写入外部存储器,则可能是其他原因,或者该权限实际上并未获得批准。 - Johnny C
我认为我的权限请求正常工作,但权限不会立即被授予。如果我进入应用程序设置,它会显示应用程序已经获得了权限,但仍然无法创建文件或文件夹。如果重新启动应用程序,则没有问题。 - Maxitors
Android 应用程序权限是使用 UID 权限处理的。权限被授予给您的应用程序运行的 UID。重新启动应用程序不会改变此 UID 或其权限设置。 - Johnny C
Joonas,你解决了吗?我也遇到了完全相同的问题。尽管ContextCompat.checkSelfPermission(this,android.Manifest.permission.WRITE_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED)返回true,但我的应用程序New Playlist Manager无法在外部sdcard上创建新文件夹。 - Theo
我更新了所有东西,问题就解决了。我认为问题出在gradle上,但我不确定。 - Maxitors
显示剩余2条评论
4个回答

5

针对 Android 10 (API 级别 29) 及更高版本的应用程序,默认情况下会被赋予对外部存储设备的范围访问权限,也称为作用域存储。这样的应用程序只能看到其特定于应用程序的目录。

  1. 临时解决方法 -

    <manifest ... > <application android:requestLegacyExternalStorage="true" ... > ...

  2. 永久性解决方法 - openFileDescriptor


2
您还需要添加READ_EXTERNAL_STORAGE权限以从设备中读取数据。
我认为这与Android在授予写入权限时自动授予读取权限有关,但可能由于某些 bug 在某些情况下不会执行。实际上,根据文档:
注意:如果您的应用程序使用WRITE_EXTERNAL_STORAGE权限,则它隐含地具有读取外部存储的权限。
在页面https://developer.android.com/training/data-storage/files.html中提到。
我曾经遇到过一种情况,在将apk安装到设备上进行测试后,我使用命令
adb shell pm grant com.blippar.ar.android.debug android.permission.WRITE_EXTERNAL_STORAGE
这触发了您观察到的行为。而同时授予前面和后面的权限
adb shell pm grant com.blippar.ar.android.debug android.permission.READ_EXTERNAL_STORAGE
解决了这个问题。

使用 ADB 授予权限解决了我在华为设备上遇到的问题,尽管已经授予了运行时权限(API 26),但仍出现权限被拒绝的错误。 - Sdghasemi

0

我一直遇到同样的问题,而且我注意到这个问题是在我升级到Gradle 3.1.0版本后立即出现的。如果你已经升级了,请尝试回退到3.0.1版本。


0

可能是在选定的构建变体中出现问题。

  1. 您在RELEASE中构建应用程序,然后运行,批准权限,编写文件
  2. 再次在DEBUG中重建应用程序,然后运行(使用早期的RELEASE变体进行卸载,因此UID可能会更改),然后尝试访问在(1)中填充的文件
  3. UID已更改,文件(路径)不受所有权,拒绝访问发生。

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