我无法获取位于我的应用文件夹之外的文件,Android Studio。

huangapple go评论106阅读模式
英文:

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

问题

我在创建文件选择窗口时遇到了一个问题,即使我的应用程序有权限处理文件,但无法获取应用程序文件夹之外的文件(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");

调试和listFiles()方法显示该文件夹不为空

但如果指定与我的应用程序无关的任何其他文件夹,比如下载文件夹,它会立即变为空,尽管那里有文件。

currentPath = Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS);
英文:

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    package=&quot;com.example.kos&quot;&gt;

    &lt;uses-permission android:name=&quot;android.permission.CAMERA&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.FOREGROUND_SERVICE&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.RECORD_AUDIO&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;

    &lt;application
        android:allowBackup=&quot;true&quot;
        android:largeHeap=&quot;true&quot;
        android:fullBackupContent=&quot;@xml/backup_descriptor&quot;
        android:icon=&quot;@mipmap/ic_launcher&quot;
        android:label=&quot;@string/app_name&quot;
        android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
        android:supportsRtl=&quot;true&quot;
        android:theme=&quot;@style/AppTheme&quot;&gt;
        &lt;service
            android:name=&quot;.Times&quot;
            android:enabled=&quot;true&quot;
            android:exported=&quot;false&quot;/&gt;

        &lt;activity
            android:name=&quot;.MainActivity&quot;
            android:configChanges=&quot;uiMode&quot;
            android:screenOrientation=&quot;fullSensor&quot;
            android:theme=&quot;@style/AppTheme.Launcher&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
    &lt;/application&gt;

&lt;/manifest&gt;

Getting permissions

if(ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED &amp;&amp; ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED &amp;&amp; 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() + &quot;/backups&quot;);

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);

答案1

得分: 1

Android 10 带来了分区存储(Scoped storage)。这就是为什么您在应用文件夹外部看不到任何文件的原因。您可以选择 opt-out 分区存储。只需将以下属性添加到您的 AndroidManifest.xml 文件中。

&lt;application&gt;
...
android:requestLegacyExternalStorage=&quot;true&quot;
...
&lt;/application&gt;

这仅是为了与 Android 10 的兼容性而提供的解决方案。对于 Android 11,您需要使用 SAF(Storage Access Framework) 访问文件。如果您的应用面向 Android 11,还需查看 这些关于 SAF 的更改

英文:

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.

&lt;application&gt;
...
android:requestLegacyExternalStorage=&quot;true&quot;
...
&lt;/application&gt;

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.

huangapple
  • 本文由 发表于 2020年8月19日 22:23:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63489111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定