英文:
Android sharing Intent choser - Filter apps based on accepted file size
问题
在我的应用程序中,我使用以下代码将多个视频文件共享到用户选择的应用程序。这个功能运行良好,但我希望动态地将选择限制在能够处理文件大小的应用程序范围内。例如,Gmail在我的设备上将附件限制为25MB,而Google Drive则没有此限制。
是否可能实现这一点?
如果有帮助的话,这是我的代码。
Intent sharingIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
ArrayList<Uri> uris = new ArrayList<>();
for(File f : files.getFiles()){ //files是要共享的文件数组
Uri mp4Uri = FileProvider.getUriForFile(context, "com.myfileproviderauthority", f);
uris.add(mp4Uri);
}
sharingIntent.setType("video/mp4");
sharingIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
sharingIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "toMe@company.com" });
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Video");
sharingIntent.putExtra(Intent.EXTRA_TEXT, "Checkout the video!");
sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Intent chooser = Intent.createChooser(sharingIntent, "Share video clip");
List<ResolveInfo> resInfoList =
context.getPackageManager().queryIntentActivities(chooser, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
for(Uri uri : uris){
//TODO: 注意:调用setFlags()是唯一一种通过临时访问权限安全地授予文件访问权限的方法。
// 避免为文件的内容URI调用Context.grantUriPermission()方法,因为此方法授予的访问权限只能通过调用Context.revokeUriPermission()方法来撤消。
context.grantUriPermission(packageName, uri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
context.startActivity(chooser);
英文:
In my application I use the following code to share multiple video files to an application of the users choice. This works fine but I would like to dynamically limit the choices to apps that can handle the filesize. For instance gmail limits it attachments to 25MB on my device, while google drive does not.
Is it possible to do this?
If it helps here is my code.
ArrayList<Uri> uris = new ArrayList<>();
for(File f : files.getFiles()){ //files is an array of the files to share
Uri mp4Uri = FileProvider.getUriForFile(context, "com.myfileproviderauthority", f);
uris.add(mp4Uri);
}
sharingIntent.setType("video/mp4");
sharingIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
sharingIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "toMe@company.com" });
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Video");
sharingIntent.putExtra(Intent.EXTRA_TEXT, "Checkout the video!");
sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Intent chooser = Intent.createChooser(sharingIntent, "Share video clip");
List<ResolveInfo> resInfoList =
context.getPackageManager().queryIntentActivities(chooser, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
for(Uri uri : uris){
//TODO: Caution: Calling setFlags() is the only way to securely grant access to your files using temporary access permissions.
// Avoid calling Context.grantUriPermission() method for a file's content URI, since this method grants access that you can only revoke by calling Context.revokeUriPermission().
context.grantUriPermission(packageName, uri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
context.startActivity(chooser);
</details>
专注分享java语言的经验与见解,让所有开发者获益!
评论