安卓分享意图选择器 – 基于接受文件大小进行应用筛选

huangapple 未分类评论46阅读模式
英文:

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&lt;Uri&gt; uris = new ArrayList&lt;&gt;();
        for(File f : files.getFiles()){ //files is an array of the files to share
            Uri mp4Uri = FileProvider.getUriForFile(context, &quot;com.myfileproviderauthority&quot;, f);
            uris.add(mp4Uri);
        }

        sharingIntent.setType(&quot;video/mp4&quot;);
        sharingIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
        sharingIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { &quot;toMe@company.com&quot; });
        sharingIntent.putExtra(Intent.EXTRA_SUBJECT, &quot;Video&quot;);
        sharingIntent.putExtra(Intent.EXTRA_TEXT, &quot;Checkout the video!&quot;);
        sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        
        Intent chooser = Intent.createChooser(sharingIntent, &quot;Share video clip&quot;);

        List&lt;ResolveInfo&gt; 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&#39;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>


huangapple
  • 本文由 发表于 2020年5月29日 15:53:31
  • 转载请务必保留本文链接:https://java.coder-hub.com/62081191.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定