Android无法创建目录

发布于 2025-02-04 08:13:10 字数 642 浏览 1 评论 0原文

我无法创建目录,我拥有所有的权限,并且在我的清单中具有所有权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

在Main Activity otCreate中,检查权限,如果它具有创建目录,但它总是返回一个错误:

if (!checkPermission()) requestPermission();
    else {
        File folder = new File(Environment.getExternalStorageDirectory() +
                File.separator + "receipts");
        if (!folder.exists()) {
            boolean bool = folder.mkdirs();
            System.out.println(bool);
        }
    }

是否有任何线索或提示为什么?谢谢

I cannot create directory, I have all the permissions and this in my Manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

In MainActivity onCreate, checks permission, if it has it should create a directory but it always returns a false:

if (!checkPermission()) requestPermission();
    else {
        File folder = new File(Environment.getExternalStorageDirectory() +
                File.separator + "receipts");
        if (!folder.exists()) {
            boolean bool = folder.mkdirs();
            System.out.println(bool);
        }
    }

Any clue or hint to why? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦欢 2025-02-11 08:13:10

不幸的是,正如@Commonsware所说,随着Android 11及以上的安全更新,您根本无法在外部存储(SDCARD)上编写目录。

直接从 docs:

访问目录

您不能再使用action_open_document_tree Atim to Action_open_document_tree行动
请求访问以下目录:

内部存储量的根目录。
设备制造商认为是可靠的每个SD卡量的根目录
被模仿或可移动。
可靠的卷是一个应用程序可以
大多数时候成功访问。
下载目录。

另外来自同一地方:

从Android 11开始的外部存储的APP特定目录,应用程序
无法在外部存储上创建自己的应用程序特定目录。
访问系统为您的应用程序提供的目录,请致电
getexternalfilesdirs()。

您的应用程序生成的系统目录可以存储任何信息。当然,这是有道理的,因为在任何给定时间,用户可以删除/格式化设备内的SD卡,并且您的应用程序的数据将完全丢失。

来自更多docs:

您会用这句话来编写:

//Write to a file
String filename = "myfile";
String fileContents = "Hello world!";
try (FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE)) {
    fos.write(fileContents.toByteArray());
}

和阅读文件:

//To read from the file
FileInputStream fis = context.openFileInput(filename

);
InputStreamReader inputStreamReader =
        new InputStreamReader(fis, StandardCharsets.UTF_8);
StringBuilder stringBuilder = new StringBuilder();
try (BufferedReader reader = new BufferedReader(inputStreamReader)) {
    String line = reader.readLine();
    while (line != null) {
        stringBuilder.append(line).append('\n');
        line = reader.readLine();
    }
} catch (IOException e) {
    // Error occurred when opening raw file for reading.
} finally {
    String contents = stringBuilder.toString();
}

这些都在您应用程序的“沙箱”文件夹中。因此,您无需声明权限。

Unfortunately, with the security updates brought by Android 11 and up, as @CommonsWare said, you simply can't write directories on external storage (sdcard).

Straight from the docs:

Access to directories

You can no longer use the ACTION_OPEN_DOCUMENT_TREE intent action to
request access to the following directories:

The root directory of the internal storage volume.
The root directory of each SD card volume that the device manufacturer considers to be reliable, regardless of whether the card
is emulated or removable.
A reliable volume is one that an app can
successfully access most of the time.
The Download directory.

Additionally from the same place:

App-specific directory on external storage Starting in Android 11, apps
cannot create their own app-specific directory on external storage. To
access the directory that the system provides for your app, call
getExternalFilesDirs().

Your app has a system generated directory to store any information. This makes sense, of course, because at any given time the user could remove/format the sd card inside the device, and your app's data would be entirely lost.

From more docs:

You would use this to write:

//Write to a file
String filename = "myfile";
String fileContents = "Hello world!";
try (FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE)) {
    fos.write(fileContents.toByteArray());
}

And to read a file:

//To read from the file
FileInputStream fis = context.openFileInput(filename

);
InputStreamReader inputStreamReader =
        new InputStreamReader(fis, StandardCharsets.UTF_8);
StringBuilder stringBuilder = new StringBuilder();
try (BufferedReader reader = new BufferedReader(inputStreamReader)) {
    String line = reader.readLine();
    while (line != null) {
        stringBuilder.append(line).append('\n');
        line = reader.readLine();
    }
} catch (IOException e) {
    // Error occurred when opening raw file for reading.
} finally {
    String contents = stringBuilder.toString();
}

These are both within your app's "sandbox" folder. Because of this, you do not need to declare permissions.

网名女生简单气质 2025-02-11 08:13:10

文件文件夹= new file(emoventer.getExternalStorageDirectory() +
file.separator +“收据”);

更改为:

 File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "receipts");

File folder = new File(Environment.getExternalStorageDirectory() +
File.separator + "receipts");

Change to:

 File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "receipts");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文