简体   繁体   English

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

[英]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我正在做一个文件选择 window 并遇到了我无法获取应用程序文件夹(com.example.kos)之外的文件的问题,即使我的应用程序可以访问文件

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) Android - 10(API 29)

UPD: Getting the path to app folder UPD:获取应用程序文件夹的路径

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

debugging and listFiles () method show that the folder is not empty调试和listFiles()方法显示文件夹不为空

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. Android 10 带有分区存储。 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.只需将下面的属性添加到您的 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 .它是仅用于与 Android 10 兼容的解决方案。对于 Android 11,您需要使用SAF访问文件。 If your app targets Android 11, check out also theese changes for SAF.如果您的应用以 Android 11 为目标,请同时检查 SAF 的这些更改

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

相关问题 我在 Android Studio 中找不到我的项目的构建文件夹 - I can't find the build folder of my project in Android studio 我无法在 Android Studio 中为我的应用设置图标 - I can't set an icon for my app in Android Studio 我在Android Studio文件夹中看不到Extras文件夹 - I can't see Extras Folder in Android Studio Folder 我的Android应用程序无法上网 - I can't get internet on my android app java.lang.UnsatisfiedLinkError…如何在Android Studio的Android应用程序中使用C ++文件? - java.lang.UnsatisfiedLinkError… How can I use C++ files in my Android App in Android Studio? 找不到我的原始文件夹(Android Studio) - Can't locate my raw folder (Android Studio) 无法获取源代码以在Android Studio的JNI文件夹中构建 - Can't get source code to build in JNI folder in Android Studio 如何解决 Android Studio 中原始文件夹的问题? - How can i fix my problem with raw folder in Android Studio? Android Studio - 我无法在实际设备上看到使用FileOutputStream创建的文件 - Android Studio - I can't see files created with FileOutputStream on my actual device 如何让Eclipse将.class文件存储在我存储.java源文件的Project文件夹之外的文件夹中? - How can I get Eclipse to store .class files in a folder outside the Project folder where I store the .java source files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM