java - Android 共享 Intent 选择器 - 根据接受的文件大小过滤应用
问题描述
在我的应用程序中,我使用以下代码将多个视频文件共享给用户选择的应用程序。这工作正常,但我想动态地将选择限制为可以处理文件大小的应用程序。例如,gmail 在我的设备上将其附件限制为 25MB,而 google drive 则没有。
是否有可能做到这一点?
如果有帮助,这里是我的代码。
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);
解决方案
推荐阅读
- spock - 放心 - 在多次重定向后验证最后一页的 url
- cmake - 找不到“Eigen3”(请求的版本 3)提供的包配置文件?
- python - 在数据框中创建一个新列,其中包含列表中该行的其他列的值
- html - 实时服务器不刷新vscode中的页面
- ios - 是否可以在 Swift 的启动屏幕中显示额外的图像和按钮
- flutter - 为什么 ListView 直接在 Column 内部使用时会导致渲染错误?
- database - 数据库设计:如何表示关系中的条件?
- c# - iText7 - 将文本添加到现有 pdf 的底部
- azure - Azure 数据工厂:如何在复制或导入期间重命名 blob 存储中的 blob csv 或文本文件?
- internet-explorer-11 - 在 IE 11 中下载之前看不到任何禁用自动提示的选项