csh_34 2013-07-01 08:48 采纳率: 0%
浏览 4472
已采纳

在 android 中分享照片时如何过滤应用程序

我使用下面的代码通过其它的程序来来分享照片,然后打印它们的包名:

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/jpeg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(FilePath));
PackageManager packageManager = this.getPackageManager();
List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(sharingIntent, PackageManager.MATCH_DEFAULT_ONLY);
int i = 0;
while(i < resolveInfo.size()) {
    System.out.println(i + "  " + resolveInfo.get(i).activityInfo.packageName);
    i++;
}
startActivity(Intent.createChooser(sharingIntent, "Share");

获取了下面的9个包名,可以分享照片:

06-29 16:55:22.460: I/System.out(13020): 0  com.amazon.kindle
06-29 16:55:22.460: I/System.out(13020): 1  com.android.bluetooth
06-29 16:55:22.460: I/System.out(13020): 2  com.google.android.apps.uploader
06-29 16:55:22.460: I/System.out(13020): 3  com.ecareme.asuswebstorage
06-29 16:55:22.460: I/System.out(13020): 4  com.google.android.talk
06-29 16:55:22.460: I/System.out(13020): 5  com.google.android.gm
06-29 16:55:22.460: I/System.out(13020): 6  com.aripollak.picturemap
06-29 16:55:22.460: I/System.out(13020): 7  com.instagram.android
06-29 16:55:22.460: I/System.out(13020): 8  com.facebook.katana

如果当我选择应用程序时想过滤一些其它的程序,例如,我想从app列表中过滤 facebook 程序,如何实现?

  • 写回答

1条回答

  • JaveZh 2013-07-01 08:56
    关注
        Intent it = new Intent(Intent.ACTION_SEND);
        it.setType("image/jpeg");
        List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(it, 0);
        if (!resInfo.isEmpty()) {
            List<Intent> targetedShareIntents = new ArrayList<Intent>();
            for (ResolveInfo info : resInfo) {
                Intent targeted = new Intent(Intent.ACTION_SEND);
                targeted.setType("image/jpeg";
                ActivityInfo activityInfo = info.activityInfo;
    
                // judgments : activityInfo.packageName, activityInfo.name, etc.
                if (activityInfo.packageName.contains("facebook") ||activityInfo.name.contains("facebook")) {
                    continue;
                }
                targeted.setPackage(activityInfo.packageName);
                targetedShareIntents.add(targeted);
            }
    
            Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
            if (chooserIntent == null) {
                return;
            }
            try {
    chooserIntent .putExtra(Intent.EXTRA_STREAM, Uri.parse(FilePath));
                startActivity(chooserIntent);
            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(this, "Can't find share component to share", Toast.LENGTH_SHORT).show();
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?