英文:
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");
但如果指定与我的应用程序无关的任何其他文件夹,比如下载文件夹,它会立即变为空,尽管那里有文件。
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
<?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);
答案1
得分: 1
Android 10 带来了分区存储(Scoped storage)。这就是为什么您在应用文件夹外部看不到任何文件的原因。您可以选择 opt-out 分区存储。只需将以下属性添加到您的 AndroidManifest.xml 文件中。
<application>
...
android:requestLegacyExternalStorage="true"
...
</application>
这仅是为了与 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.
<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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论