Skip to content

Instantly share code, notes, and snippets.

@k3170makan
Created February 15, 2018 23:38
Show Gist options
  • Save k3170makan/c89849ff0be2fe3e142c82845cb20bf9 to your computer and use it in GitHub Desktop.
Save k3170makan/c89849ff0be2fe3e142c82845cb20bf9 to your computer and use it in GitHub Desktop.
Code granting permissions based on app names
public static void grantAppPermission(Context context, Intent intent, Uri fileUri) {
List<ResolveInfo> resolvedIntentActivities = context.getPackageManager()
.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolvedIntentInfo : resolvedIntentActivities) {
String packageName = resolvedIntentInfo.activityInfo.packageName;
context.grantUriPermission(packageName, fileUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment