保存文本文件 - 分区存储 Android 11

Saving Text Files - Scoped Storage Android 11

有没有办法在外部存储的 Android/data 文件夹之外创建和保存文本文件?我正在创建一个音乐应用程序,并添加了一个预设管理器,以便用户可以保存、加载和编辑预设文件(在 Android 11 之前工作得很好),我不希望这些文件在应用程序出现时自动删除被卸载。这就像如果 Photoshop 在卸载 Photoshop 时删除了所有 Photoshop 文档,那太糟糕了!这些是用户保存的文件,如果用户愿意,可以单独删除它们。

一定有办法解决这个问题,但我一直没能找到任何有效的方法。 ACTION_OPEN_DOCUMENT_TREE 看起来很有前途,直到我看到 Android 也删除了这个选项。

https://developer.android.com/about/versions/11/privacy/storage

You can no longer use the ACTION_OPEN_DOCUMENT_TREE intent action to request access to the following directories:

  • The root directory of the internal storage volume.
  • The root directory of each SD card volume that the device manufacturer considers to be reliable, regardless of whether the card is emulated or removable. A reliable volume is one that an app can successfully access most of the time.
  • The Download directory.

我读过 Google Play 仅允许 MANAGE_EXTERNAL_STORAGE 在需要它的应用程序(如文件浏览器或防病毒软件等)中使用,这可能不适用于我的情况。我不想只针对年龄较大的 API,所以 requestLegacyExternalStorage 也行不通。

我调查过的一切似乎都是死胡同。还有什么我可以做的吗?

这里是一个简短的测试程序(我用的是LibGDX),目前只能保存到根目录:

Android/data/com.mygdx.filetest/files/

[核心]FileTest.java

package com.mygdx.filetest;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Files;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.ScreenUtils;

public class FileTest implements ApplicationListener {
    private NativePermissions permissions;
    private Files.FileType fileType;
    private String directory = "TestDir/";
    private String name = "text.txt";
       
    public FileTest(final NativePermissions permissions){     
        this.permissions = permissions;
    }
    
    @Override
    public void create(){
        fileType = getFileType();
        if (permissions != null){
            permissions.checkExternalStoragePermission();
        } else {
            permissionGranted();
        }
    }
    private Files.FileType getFileType(){
        switch(Gdx.app.getType()) {
            case Android:
                return Files.FileType.External;
            default:
                return Files.FileType.Local;
        }
    }
    
    @Override public void render(){ ScreenUtils.clear(0.4f, 0.4f, 0.4f, 1); }
    @Override public void resize(int width, int height) {}
    @Override public void pause(){}
    @Override public void resume(){}
    @Override public void dispose (){}
    
    public void permissionGranted() {
        FileHandle fileHandle = Gdx.files.getFileHandle(directory+name, fileType); 
        if (fileHandle!=null) fileHandle.writeString("test", false);
    }
}

[android] AndroidLauncher.java

package com.mygdx.filetest;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;

public class AndroidLauncher extends AndroidApplication {
    private final FileTest application;
    private final int STORAGE_PERMISSION_CODE = 1;
    private boolean dialogBoxShowing = false;
    
    public AndroidLauncher(){
        final AndroidPermissions permissions = new AndroidPermissions(this);
        application = new FileTest(permissions);
    }
    
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        initialize(application, config);
    }
    @Override
    public void onRequestPermissionsResult(final int requestCode, final String permissions[], final int[] grantResults) {
        dialogBoxShowing = false;
        if (requestCode == STORAGE_PERMISSION_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "Permission GRANTED", Toast.LENGTH_SHORT).show();
                permissionGranted();
            } else {
                Toast.makeText(this, "Permission DENIED", Toast.LENGTH_SHORT).show();
            }
        }
    }
    public void promptExternalStoragePermission() {
        if (dialogBoxShowing) return;
        dialogBoxShowing = true;
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                final AlertDialog.Builder builder = new AlertDialog.Builder(AndroidLauncher.this);
                builder.setMessage("To save user presets and custom settings, allow access to your phone’s storage.");
                builder.setCancelable(false);
                // reverse these buttons to put "NO" on left and "YES" on right
                builder.setPositiveButton("NOT NOW", new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialogBoxShowing = false;
                        dialog.dismiss();
                    }
                });
                builder.setNegativeButton("CONTINUE", new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ActivityCompat.requestPermissions(AndroidLauncher.this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
                    }
                });
                builder.create().show();
            }
        });
    }
    public void permissionGranted(){ application.permissionGranted(); }
}

[核心]NativePermissions.java

package com.mygdx.filetest;

public interface NativePermissions {
    public void checkExternalStoragePermission();
}

[android] AndroidPermissions.java

package com.mygdx.filetest;

import android.Manifest;
import android.content.pm.PackageManager;

import androidx.core.content.ContextCompat;

public class AndroidPermissions implements NativePermissions {
    private final AndroidLauncher context;
    
    public AndroidPermissions(final AndroidLauncher context){
        this.context = context;
    }
    
    @Override
    public void checkExternalStoragePermission() {
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){
            context.permissionGranted();
        } else {
            context.promptExternalStoragePermission();
        }
    }
}

[android] AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mygdx.filetest">
    
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:isGame="true"
        android:appCategory="game"
        android:label="@string/app_name"
        android:theme="@style/GdxTheme" >
        <activity
            android:name="com.mygdx.filetest.AndroidLauncher"
            android:label="@string/app_name" 
            android:screenOrientation="fullUser"
            android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenSize|screenLayout">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

大惊小怪。

您可以以经典方式将文件保存到 public Documents 目录。

或者对该目录使用带 ACTION_OPEN_DOCUMENT_TREE 的 SAF。

两者都不需要 'all files access'。