Home  >  Article  >  Can't create directories or files for my application even though I have permissions

Can't create directories or files for my application even though I have permissions

王林
王林forward
2024-02-22 13:28:14340browse

php editor Strawberry will answer common questions in Java programming: Even if you have permissions, you cannot create a directory or file for the application. This problem may occur in operating system permission settings, file path errors, files already existing, etc. By checking the file path, checking operating system permissions, and ensuring that the file does not exist, you can solve this problem and ensure that the program runs normally. If you encounter a similar situation, you might as well try the above methods to see if they can solve your problem.

Question content

I have an app I made a few years ago. Recently I just recompiled it with very few changes so that I could change the target sdk and still get it listed in the play store.

Turns out this fundamentally breaks the app in some phones (including my own). Before uploading, I tested it on another phone and it worked fine (android version 8.1.0). My phone is running android 11 (doesn't work).

The problem seems to be with the ability to create and read files in internal storage, whereas the focus of the application is maintaining a single file.

This is the function I try to create the directory. This is just a test function that I call on creation, it's not part of the original application:

public static void AttemptDirCreate(){
    String dirname = Environment.getExternalStorageDirectory().toString() + "/this_is_a_test";
    File dir = new File(dirname);

    System.err.println("Attempting to create directory: '" + dirname + "'");

    boolean make_result = false;
    if (!dir.exists()) {
        try {
            make_result = dir.mkdir();
        }
        catch (SecurityException se) {
            System.err.println("ERROR. Could not make dir. Reason: " + se.getMessage());
        }
    }

    if (make_result){
        System.err.println("MKDir result is true");
    }
    else {
        System.err.println("MKDir result is false");
    }

    if (dir.exists()){
        System.err.println("Dir has been created");
    }
    else {
        System.err.println("Was unable to create dir");
    }

}

The problem is that the directory is not created and no exception is thrown. When I go into the application information I see the permissions for files and media.

The path to try to create the directory is: '/storage/emulated/0/this_is_a_test'

As far as I know, this is the correct path. The app did ask me for permission when first installing it.

So why can't it create a directory? How to identify the problem and fix it?

To clarify, this exact same feature works on android 8.1.0, but not on android 11. Not sure, what the cutoff is.

Workaround

You cannot create files and directories in the external storage root of an Android 11 device.

Instead use one of the existing public directories.

Like DCIM, pictures, documents...

The above is the detailed content of Can't create directories or files for my application even though I have permissions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete