Android 权限已授予但权限仍被拒绝

Max*_*ors 4 permissions android android-6.0-marshmallow

我的项目有 3 项不同的活动。第一个活动包含所有权限请求。应用程序已授予所有权限,但仍然无法在 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();
}
Run Code Online (Sandbox Code Playgroud)

}

以上是我的许可请求活动。我很抱歉英语不好。

小智 5

默认情况下,面向 Android 10(API 级别 29)及更高版本的应用会获得对外部存储设备或范围存储的有限访问权限。此类应用程序只能看到其应用程序特定的目录

  1. 临时修复-

    <清单 ... > <应用程序 android:requestLegacyExternalStorage="true" ... > ...

  2. 永久修复- openFileDescriptor