Table of Contents
Question content
Workaround
Home Java 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

Feb 22, 2024 pm 01:28 PM

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");
    }

}
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)