[英]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.