Search code examples
androidsdkcreate-directory

Android does not allow directory creation with SDK update


Strange issue, but on one device Android no longer lets the app create a directory (or any directories). This seems to have happened since I moved to SDK 27 (I brought in a 3rd party library that requires the upgrade). Even stranger, it works on other devices. The device that does not work is a Samsung Galaxy Tab S3, running Android 8 (sdk 26).

Here is the code that fails:

public static String mediaStorageDirectory() {
    return Environment.getExternalStorageDirectory().toString() + File.separator + "myapp";
}

private DatabaseHelper(Context context) {
    super(context, DB_NAME, null, 1); 
    File dbPath = new File(Utilities.mediaStorageDirectory() + "/databases/");
    if (!dbPath.exists()) {
        if (dbPath.mkdirs()) {
            this.mContext = context;
            createDataBase();
        }
    }
}

The code creates a directory, then copies the app DB in into that directory. This code has been working since the dawn of time...has Android changed some security requirements for file creation?

If you are wondering, I have set privileges as follows:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

And the user is prompted for those privileges successfully before the creation of the directory structure.


Solution

  • So the problem seems to be related to SDK 27. Seems they are now requiring (enforcing?) that the permissions for WRITE be granted. Previously only READ was required. I would assume that this was related to security upgrades, but I could not find any documentation on this change. Perhaps everyone else just did the inclusion previously. For those wanting to see code that does the permissions handling, I pulled the following code from another Stack Overflow a while back. I merely added the WRITE_EXTERNAL_STORAGE part that was previously missing:

    final private int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124;
    
    public void checkPermissions() {
        List<String> permissionsNeeded = new ArrayList<String>();
    
        final List<String> permissionsList = new ArrayList<String>();
        if (!addPermission(permissionsList, Manifest.permission.CAMERA))
            permissionsNeeded.add("Camera");
        if (!addPermission(permissionsList, Manifest.permission.READ_EXTERNAL_STORAGE))
            permissionsNeeded.add("Read External Storage");
        if (!addPermission(permissionsList, Manifest.permission.WRITE_EXTERNAL_STORAGE))
            permissionsNeeded.add("Write External Storage");
        if (!addPermission(permissionsList, Manifest.permission.ACCESS_FINE_LOCATION))
            permissionsNeeded.add("GPS");
        if (!addPermission(permissionsList, Manifest.permission.RECORD_AUDIO))
            permissionsNeeded.add("Record Audio");
    
        if (permissionsList.size() > 0) {
            if (permissionsNeeded.size() > 0) {
                // Need Rationale
                String message = "You need to grant access to " + permissionsNeeded.get(0);
                for (int i = 1; i < permissionsNeeded.size(); i++) {
                    message = message + ", " + permissionsNeeded.get(i);
                }
    
                message = message + "." + " If you do not the application may not function correctly.";
    
                showMessageOKCancel(message, this,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
                                        REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
                            }
                        });
            } else {
                requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
                        REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
            }
        }
    }
    
    private static void showMessageOKCancel(String message, Activity activity, DialogInterface.OnClickListener okListener) {
        new AlertDialog.Builder(activity).setMessage(message).setPositiveButton("OK", okListener).create().show();
    }
    
    private boolean addPermission(List<String> permissionsList, String permission) {
        if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
            permissionsList.add(permission);
            // Check for Rationale Option
            if (!shouldShowRequestPermissionRationale(permission))
                return false;
        }
        return true;
    }
    
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS: {
                Map<String, Integer> perms = new HashMap<String, Integer>();
                // Initial
                perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
                perms.put(Manifest.permission.CAMERA, PackageManager.PERMISSION_GRANTED);
                perms.put(Manifest.permission.RECORD_AUDIO, PackageManager.PERMISSION_GRANTED);
                perms.put(Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
                perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
                // Fill with results
                for (int i = 0; i < permissions.length; i++)
                    perms.put(permissions[i], grantResults[i]);
                // Check for ACCESS_FINE_LOCATION
                if (perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                        && perms.get(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED
                        && perms.get(Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED
                        && perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
                        && perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                    // All Permissions Granted
                }
                // Warn the user that the application will not run
                else if (perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(StartActivity.this);
                    builder.setTitle("File Read Permissions Not Granted");
                    builder.setMessage("The Application cannot operate without READ access to files on your device. Do you want to grant this access?");
                    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ActivityCompat.requestPermissions(StartActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
                        }
                    });
                    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
    
                        }
                    });
                    if (!isFinishing())
                        builder.show();
                }
                // Warn the user that the application will not run
                else if (perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(StartActivity.this);
                    builder.setTitle("File Write Permissions Not Granted");
                    builder.setMessage("The Application cannot operate without WRITE access to directories on your device. Do you want to grant this access?");
                    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ActivityCompat.requestPermissions(StartActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
                        }
                    });
                    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
    
                        }
                    });
                    if (!isFinishing())
                        builder.show();
                }else {
                    // Permission Denied
                    Toast.makeText(StartActivity.this, "Some Permission is Denied", Toast.LENGTH_SHORT)
                            .show();
                }
            }
            break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
    

    The call is SDK specific, added to the OnCreate() of your main activity:

        // Check permissions for Android 6.0 devices
        if (Build.VERSION.SDK_INT >= 23) {
            // Marshmallow+
            checkPermissions();
        }