无法在 Android 6 及更低版本的个人资料所有者中将文件从个人资料共享到工作资料。
我已经编写了代码来请求和共享从个人资料到个人资料所有者应用程序中的工作资料的文件。我从个人资料所有者应用程序向个人资料中的同一应用程序实例请求文件。我在 onActivityResult 中得到结果。我得到了我处理和保存的文件的内容 Uri。该代码在 android 7 及更高版本中运行良好,但在 Android 6 及更低版本中引发 FileNotFound:Permission Denied 异常。
Intent resultIntent = new
Intent(RESULT);
Uri uri = FileProviderHelper.getUriForFile(this, file);
if (uri == null) {
Logger.w(LOG_TAG, "Log file not found");
LogCollectorActivity.this.setResult(RESULT_CANCELED,
resultIntent);
Logger.e(LOG_TAG, " Unable to create Uri
from file");
return;
}
resultIntent.setDataAndType(uri,
getContentResolver().getType(uri));
resultIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
LogCollectorActivity.this.setResult(Activity.RESULT_OK,
resultIntent);
finish();
在个人资料所有者方面,我在 onActivityResult 中收到结果
@Override
protected void onActivityResult(int requestCode, int resultCode,
@Nullable Intent data)
{
if (requestCode == MIGRATE_LOGS && resultCode ==
RESULT_OK) {
migrateLogs(data);
}
updateStateAndFinish();
}
private void migrateLogs(Intent data)
{
if (data != null) {
Uri dataUri = data.getData();
if (dataUri != null) {
try {
ParcelFileDescriptor fileDescriptorParcel =
getContentResolver().openFileDescriptor(dataUri, "r");
...
}
ParcelFileDescriptor fileDescriptorParcel = getContentResolver().openFileDescriptor(dataUri, "r"); 在这里我得到 FileNotFoundException: Permission denied。
我尝试使用 context.grantUriPermission() 授予权限,但这也不起作用。