ACTION_CREATE_DOCUMENT 第三方访问授予临时权限

1,AndroidManifest注册provider

    

2,xml文件申请根目录权限



    
    
    

3,调用封装intent

Intent intent = new Intent();
try {
    Path paths = Paths.get(file.toURI());
    Uri uri=null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        String uriPath = Uri.encode(paths.toUri().toString());
        uri = new Uri.Builder()
                .scheme(ContentResolver.SCHEME_CONTENT)
                .authority(BuildConfig.APPLICATION_ID)
                .path(uriPath)
                .build();
    } else {
        // PackageInstaller only supports file URI before N.
        uri = Uri.fromFile(paths.toFile());
    }
    int flags = Intent.FLAG_GRANT_READ_URI_PERMISSION;
        flags |= Intent.FLAG_GRANT_WRITE_URI_PERMISSION
                | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION;
    if (mFileItem.isfIsDir()) {
        flags |= Intent.FLAG_GRANT_PREFIX_URI_PERMISSION;
    }
    intent.addFlags(flags);
    intent.setData(uri);
    activity.setResult(Activity.RESULT_OK, intent);
    activity.finish();

你可能感兴趣的:(安卓开发,android,java,开发语言)