Android:为什么Intent.EXTRA_LOCAL_ONLY会显示Google相册

Gir*_*hai 3 android android-gallery

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

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);
Run Code Online (Sandbox Code Playgroud)

根据Intent.EXTRA_LOCAL_ONLY不起作用

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

添加intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);上面的代码后,它隐藏了谷歌驱动器应用程序picasa应用程序,但仍显示谷歌照片(这些照片不在我的设备中.)

此外,我只为本地文件尝试了Android图像选择器,但它隐藏了所有具有远程图像的应用程序,不包括谷歌照片.

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

所以我的问题是否有任何方法隐藏谷歌照片应用程序,只从本地设备选择图像.或谷歌照片是否属于Intent.EXTRA_LOCAL_ONLY

Nuw*_*sam 11

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

Google+照片会同时存储本地和远程图片,因此会使用该图片注册该图片.但显然它忽略了调用intent EXTRA_LOCAL_ONLY设置为true的地方.

您可以尝试手动从列表中删除G +照片(但这似乎有点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);
Run Code Online (Sandbox Code Playgroud)

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

代码段是此链接的修改版本.

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