我无法获取我的应用程序文件夹 Android studio 之外的文件

I can't get files that are outside my app folder Android studio

我在进行文件选择 window 和 运行 时遇到了无法获取应用程序文件夹之外的文件的问题 (com.example.kos) ,即使我的应用程序有权使用文件

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.kos">

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:largeHeap="true"
        android:fullBackupContent="@xml/backup_descriptor"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <service
            android:name=".Times"
            android:enabled="true"
            android:exported="false"/>

        <activity
            android:name=".MainActivity"
            android:configChanges="uiMode"
            android:screenOrientation="fullSensor"
            android:theme="@style/AppTheme.Launcher">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

正在获取权限

if(ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(context, Manifest.permission.FOREGROUND_SERVICE) != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions((Activity) context,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.FOREGROUND_SERVICE},REQUEST_CODE_FOLDER_CONF);
                    }

Android - 10 (api 29)

更新: 正在获取应用程序文件夹的路径

File currentPath = new File(context.getExternalFilesDir(null).getAbsolutePath() + "/backups");

debugging and listFiles () method show that the folder is not empty

但是如果指定任何其他与我的应用程序无关的文件夹,例如下载文件夹,it immediately becomes empty,尽管文件在那里

currentPath = Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS);

Android 10 带有 Scoped 存储。这就是为什么您在应用程序文件夹之外看不到任何文件的原因。您可以 opt-out 分区存储。只需将下面的属性添加到您的 AndroidManifest.xml.

<application>
...
android:requestLegacyExternalStorage="true"
...
</application>

这是一个仅用于与 Android 10 兼容的解决方案。对于 Android 11,您需要使用 SAF. If your app targets Android 11, check out also theese changes 访问 SAF 的文件。