0

因此,我已将我的应用程序定义为接受其他应用程序以将其作为图库打开以选择照片。

        <intent-filter>
            <action android:name="android.intent.action.PICK" />
            <action android:name="android.intent.action.GET_CONTENT" />

            <data android:mimeType="image/*" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.OPENABLE" />
        </intent-filter>

但是,在我的应用程序中,我还希望允许用户从图库中选择图像。

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, REQ_CODE_PICK_FROM_GALLERY);

您可能会想到,问题是我的应用程序出现在选择器对话框中。是否可以过滤掉我自己的应用程序?

谢谢。

4

1 回答 1

2

最后我通过使用这段代码解决了。 https://gist.github.com/mediavrog/5625602

// Usage:
// blacklist
String[] blacklist = new String[]{"com.any.package", "net.other.package"};
// your share intent
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "some text");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "a subject");
// ... anything else you want to add
// invoke custom chooser
startActivity(generateCustomChooserIntent(intent, blacklist));

// Method:
private Intent generateCustomChooserIntent(Intent prototype, String[] forbiddenChoices) {
    List<Intent> targetedShareIntents = new ArrayList<Intent>();
    List<HashMap<String, String>> intentMetaInfo = new ArrayList<HashMap<String, String>>();
    Intent chooserIntent;

    Intent dummy = new Intent(prototype.getAction());
    dummy.setType(prototype.getType());
    List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(dummy, 0);

    if (!resInfo.isEmpty()) {
        for (ResolveInfo resolveInfo : resInfo) {
            if (resolveInfo.activityInfo == null || Arrays.asList(forbiddenChoices).contains(resolveInfo.activityInfo.packageName))
                continue;

            HashMap<String, String> info = new HashMap<String, String>();
            info.put("packageName", resolveInfo.activityInfo.packageName);
            info.put("className", resolveInfo.activityInfo.name);
            info.put("simpleName", String.valueOf(resolveInfo.activityInfo.loadLabel(getPackageManager())));
            intentMetaInfo.add(info);
        }

        if (!intentMetaInfo.isEmpty()) {
            // sorting for nice readability
            Collections.sort(intentMetaInfo, new Comparator<HashMap<String, String>>() {
                @Override
                public int compare(HashMap<String, String> map, HashMap<String, String> map2) {
                    return map.get("simpleName").compareTo(map2.get("simpleName"));
                }
            });

            // create the custom intent list
            for (HashMap<String, String> metaInfo : intentMetaInfo) {
                Intent targetedShareIntent = (Intent) prototype.clone();
                targetedShareIntent.setPackage(metaInfo.get("packageName"));
                targetedShareIntent.setClassName(metaInfo.get("packageName"), metaInfo.get("className"));
                targetedShareIntents.add(targetedShareIntent);
            }

            chooserIntent = Intent.createChooser(targetedShareIntents.remove(targetedShareIntents.size() - 1), getString(R.string.share));
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
            return chooserIntent;
        }
    }

    return Intent.createChooser(prototype, getString(R.string.share));
}
于 2013-07-10T05:48:07.853 回答