简体   繁体   中英

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

I was doing a file selection window and ran into the problem that I can't get files that are outside the application folder (com.example.kos) , even though my application has access to work with files

Manifest

<?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>

Getting permissions

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)

UPD: Getting the path to app folder

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

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

But if specify any other folder not related to my application, such as the download folder, it immediately becomes empty , although files are there

currentPath = Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS);

Android 10 comes with Scoped storage. This is the reason why you don't see any files outside the app folder. You can opt-out scoped storage. Just add attribute below to your AndroidManifest.xml.

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

It is a solution only for compatibility with Android 10. For Android 11 you need to access files with SAF . If your app targets Android 11, check out also theese changes for SAF.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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