[英]Android MediaPlayer: File Access in Android Q and above
[英]Determine existing file access permissions on Android Q
我正在从本机 C++ 代码读取和写入文件和目录。 获取文件访问的标准方法是使用 Java 中的存储访问框架,然后使用本机代码中的文件描述符( https://developer.android/medianative/-datatorage/files.com/ ):
// 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");
}
onActivityResult
返回的 Uri 可以存储和重新获取,这样我们就不必每次都提示用户( 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 的情况下访问:
onActivityResult
结果中存储的 Uri?myNativeCallbackFunction
function 方法更漂亮的方式将文件描述符传输为本机代码?问候,
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
删除该行。 这没有道理。 你不能在那里授予任何东西。
getFilesDir(), getExternalFilesDir() and getExternalFilesDirs()
是您仍然可以使用经典 File 类的地方。
此外:原则上,您不必像使用getPermanentUriPermissions()
那样存储获得的 uri,您始终可以看到它们。
但是,如果您拥有多个权限,您将不知道哪个权限是针对哪个权限的。 所以也需要存储 self 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.