简体   繁体   English

无法在 android 内部存储中创建文件夹

[英]Unable to create folder in android internal storage

I'm trying to create a folder in the internal storage of the phone but it just doesn't create it no matter what i do我正在尝试在手机的内部存储中创建一个文件夹,但无论我做什么它都不会创建它

Things I've tried:我尝试过的事情:

  • unplugging the phone from the computer从电脑上拔下手机

  • set permissions both in manifest and programatically for API 23+在清单中和以编程方式为 API 23+ 设置权限

  • every single variation of folder getting i could find: Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) Environment.getDataDirectory() etc..我能找到的每个文件夹变体:Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) Environment.getDataDirectory() 等。

  • searched every single article or variation of the article for similar problems搜索每篇文章或文章的变体以查找类似问题

  • tried both variations of mkdir() and mkdirs()尝试了 mkdir() 和 mkdirs() 的两种变体

  • checked phone if folder exists检查手机是否存在文件夹

This code is also tied to a button so the permission asking should already be granted the second time i press it.此代码也与一个按钮相关联,因此在我第二次按下它时应该已经授予了权限请求。

File sdir = new File (Environment.getExternalStorageDirectory().getpath(), "/DBTEST/");

String[] PERMISSIONS_STORAGE = {
        Manifest.permission.WRITE_EXTERNAL_STORAGE
};
ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS_STORAGE, 1);

System.out.println("DIRECTORY CREATED: " + sdir.mkdirs());
System.out.println("EXISTS: " + sdir.exists());
System.out: DIRECTORY CREATED: false
System.out: EXISTS: false
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.testM">

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

Try changing this line:尝试更改此行:

File sdir = new File (Environment.getExternalStorageDirectory().getpath(), "/DBTEST/");

to

File sdir = new File (Environment.getExternalFilesDir(null) + "/DBTEST/");

According to the developer docs:根据开发者文档:

getExternalStorageDirectory() - This method was deprecated in API level 29. To improve user privacy, direct access to shared/external storage devices is deprecated. getExternalStorageDirectory() - 此方法在 API 级别 29 中已弃用。为了提高用户隐私,不建议直接访问共享/外部存储设备。 When an app targets Build.VERSION_CODES.Q, the path returned from this method is no longer directly accessible to apps.当应用程序以 Build.VERSION_CODES.Q 为目标时,从此方法返回的路径不再可供应用程序直接访问。 Apps can continue to access content stored on shared/external storage by migrating to alternatives such as Context#getExternalFilesDir(String), MediaStore, or Intent#ACTION_OPEN_DOCUMENT.通过迁移到 Context#getExternalFilesDir(String)、MediaStore 或 Intent#ACTION_OPEN_DOCUMENT 等替代方案,应用程序可以继续访问存储在共享/外部存储上的内容。

getExternalFilesDir(null) will return your apps storage folder at (Internal Storage)/Android/data/your.app.name/file/ getExternalFilesDir(null)将在 (Internal Storage)/Android/data/your.app.name/file/ 返回您的应用程序存储文件夹

If you want to access certain OS defined folders you just pass environmental constants defined here:如果您想访问某些操作系统定义的文件夹,您只需传递此处定义的环境常量:

https://developer.android.com/reference/android/content/Context#getExternalFilesDir(java.lang.String) https://developer.android.com/reference/android/content/Context#getExternalFilesDir(java.lang.String)

and here和这里

https://developer.android.com/reference/android/os/Environment https://developer.android.com/reference/android/os/Environment

under the "Fields" section.在“字段”部分下。

Ex.前任。 downloads would be: getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)下载将是: getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)

This is a privacy restriction introduced in Android-Q.这是 Android-Q 中引入的隐私限制。 Direct access to shared/external storage devices is deprecated when an app targets API 29 and the path returned from getExternalStorageDirectory method is no longer directly accessible to apps.当应用程序以 API 29 为目标并且从getExternalStorageDirectory方法返回的路径不再可供应用程序直接访问时,不推荐直接访问共享/外部存储设备。 Use the app-specific directory to write & read files.使用特定于应用程序的目录来写入和读取文件。

By default, apps targeting Android 10 and higher are given scoped access into external storage , or scoped storage.默认情况下,针对 Android 10 及更高版本的应用程序被授予对外部存储或范围存储的范围访问权限 Such apps can see the following types of files within an external storage device without needing to request any storage-related user permissions:此类应用程序可以在外部存储设备中查看以下类型的文件,而无需请求任何与存储相关的用户权限:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM