首页 > 解决方案 > 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);

标签: javaandroidandroid-intentandroid-sharing

解决方案


推荐阅读