首页 > 解决方案 > 确定 Android Q 上现有的文件访问权限

问题描述

我正在从本机 C++ 代码读取和写入文件和目录。获取文件访问权限的标准方法是使用 Java 中的存储访问框架,然后在本机代码中使用文件描述符(https://developer.android.com/training/data-storage/files/media#native-code ):

// Called from native code:
public void requestFile() {
    Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    startActivityForResult(intent, CREATE_REQUEST_CODE);
}

public native myNativeCallbackFunction(int fd, ...);

public void onActivityResult(int requestCode,
        int resultCode, Intent resultData) {

    Uri uri = resultData.getData();
    ParcelFileDescriptor parcelFd = resolver.openFileDescriptor(uri, "r");
    int fd = parcelFd.detachFd();
    myNativeCallbackFunction(fd, "other information identifying the file such as the file name");
}

返回的 Uri fromonActivityResult可以存储和重新获取,这样我们就不必每次都提示用户(https://developer.android.com/guide/topics/providers/document-provider#permissions):

final int takeFlags = intent.getFlags()
            & (Intent.FLAG_GRANT_READ_URI_PERMISSION
            | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
// Check for the freshest data.
getContentResolver().takePersistableUriPermission(uri, takeFlags);

但是有些位置(app.package/files例如)可以在没有 SAF 的情况下访问:

  1. 有没有办法确定是否可以在没有 SAF 的情况下访问文件/目录并且没有过去onActivityResult结果中存储的 Uri?
  2. 有没有比使用函数方法更漂亮的将文件描述符传输到本机代码的myNativeCallbackFunction方法?

问候,

标签: androidandroid-ndkfile-accessandroid-10.0storage-access-framework

解决方案


   intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

删除该行。这没有道理。你不能在那里授予任何东西。

getFilesDir(), getExternalFilesDir() and getExternalFilesDirs()是您仍然可以使用经典 File 类的地方。

进一步:原则上,您不必存储获得的 uri,因为getPermanentUriPermissions()您可以随时看到它们。

但是,如果您拥有多个权限,您将不知道哪个权限是针对哪个权限的。所以也需要存储 self 。


推荐阅读