Android:为什么 Intent.EXTRA_LOCAL_ONLY 显示 Google 相册 [英] Android : Why Intent.EXTRA_LOCAL_ONLY shows Google Photos

查看:30
本文介绍了Android:为什么 Intent.EXTRA_LOCAL_ONLY 显示 Google 相册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中尝试做的是让用户从他手机的图库中选择一张图片(不想 仅获取图库图像,但也允许用户选择他们选择的应用程序).我使用的代码如下:

What I'm trying to do in my application is to let the user choose a picture from his phone's gallery(Don't want to get gallery images only but also allowing a user to choose their app of choice). The code I'm using is as follows:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);

根据 Intent.EXTRA_LOCAL_ONLY 不起作用

EXTRA_LOCAL_ONLY 只告诉接收应用它应该返回仅存在的数据.

EXTRA_LOCAL_ONLY only tells the receiving app that it should return only data that is present.

在上面的代码中添加 intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true); 后,它隐藏了 google drive apppicasa app 但仍然显示 google 照片(这些照片不在我的设备中.)

After adding intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true); in above code,it hide google drive app and picasa app but still shows google photos(those photos are not in my device.)

我也试过 Android 图像选择器仅用于本地文件 但是它隐藏了所有具有远程图像的应用程序,不包括谷歌照片.

Also I tried Android image picker for local files only but It hide all the apps having remote images excluding google photos.

注意:所有图像路径都是正确的,就像我做的那样 KitKat 上的 Android Gallery 为 Intent.ACTION_GET_CONTENT 返回不同的 Uri(感谢@Paul Burke)但我不想选择互联网/远程图像.

Note : All the image paths are correct as I did Android Gallery on KitKat returns different Uri for Intent.ACTION_GET_CONTENT (thanks to @Paul Burke) but I don't want to pick internet/remote images.

所以我的问题是否有任何方法可以在仅从本地设备选择图像的同时隐藏谷歌照片应用程序.或者谷歌照片是 Intent.EXTRA_LOCAL_ONLY 的一部分吗?>

So my question Is there any way to hide google photos app while pick an image from local device only. or Is google photos is part of Intent.EXTRA_LOCAL_ONLY

推荐答案

EXTRA_LOCAL_ONLY 只告诉接收应用它应该返回仅存在的数据.

Google+ 照片存储本地和远程图片,因此使用额外的意图注册.然而,显然它会忽略任何将 EXTRA_LOCAL_ONLY 设置为 true 的调用意图.

Google+ Photos stores both local and remote pictures and because of that is registered for that intent with that extra. However apparently it ignores wherever calling intent has EXTRA_LOCAL_ONLY set to true.

您可以尝试手动从列表中删除 G+ 照片(但这似乎有点麻烦):

You could try removing G+ Photos from list manually (however this seems a bit hacky):

List<Intent> targets = new ArrayList<Intent>();
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
List<ResolveInfo> candidates = getPackageManager().queryIntentActivities(intent, 0);

for (ResolveInfo candidate : candidates) {
  String packageName = candidate.activityInfo.packageName;
  if (!packageName.equals("com.google.android.apps.photos") && !packageName.equals("com.google.android.apps.plus") && !packageName.equals("com.android.documentsui")) {
      Intent iWantThis = new Intent();
      iWantThis.setType("image/*");
      iWantThis.setAction(Intent.ACTION_GET_CONTENT);
      iWantThis.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
      iWantThis.setPackage(packageName);
      targets.add(iWantThis);
    }
}
Intent chooser = Intent.createChooser(targets.remove(0), "Select Picture");
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, targets.toArray(new Parcelable[targets.size()]));
startActivityForResult(chooser, 1);

几句话的解释:targets.remove(0) 将从 targets 列表中删除并返回第一个 Intent,因此选择器将只包含一个应用程序.然后用 Intent.EXTRA_INITIAL_INTENTS 添加其余部分.

Few words of explanation: targets.remove(0) will remove and return first Intent from targets list, so the chooser will consist only of one application. Then with Intent.EXTRA_INITIAL_INTENTS we add the rest.

代码片段是从这个链接.

请记住检查所有条件,例如是否至少有一个应用程序可用等等.

Please remember to check all conditions like if there is at least one app available and so on.

这篇关于Android:为什么 Intent.EXTRA_LOCAL_ONLY 显示 Google 相册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆