安卓:为什么Intent.EXTRA_LOCAL_ONLY显示谷歌图片 [英] Android : Why Intent.EXTRA_LOCAL_ONLY shows Google Photos

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

问题描述

我想要在我的应用程序做的是让用户选择从他的手机图库中的图片(不要想的拿到画廊图像只而且还允许用户选择自己所选择的应用程序)。在code我使用如下:

 意向意图=新的Intent();
intent.setType(图像/ *);
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_LOCAL_ONLY,真);
startActivityForResult(Intent.createChooser(意向,选择图片),1);

作为每个<一href=\"http://stackoverflow.com/questions/20620794/intent-extra-local-only-doesnt-work\">Intent.EXTRA_LOCAL_ONLY不工作


  

EXTRA_LOCAL_ONLY 只告诉接收应用程序,它应该返回
  唯一的数据是present。


添加 intent.putExtra(Intent.EXTRA_LOCAL_ONLY,真)后,上述code ,它隐藏的谷歌驱动器应用和< STRONG>的Picasa应用,但仍然显示的 Google相册(这些照片是不是在我的设备)。

我也试过 Android的图像选择器对本地文件只但它隐藏其不包括谷歌的照片远程图像所有的应用程序。

请注意:所有图像路径是正确的,因为我没有 Android图库上奇巧的Intent.ACTION_GET_CONTENT 返回不同的URI(感谢@保罗·伯克),但我不想挑互联网/远程图像。

所以我的问题的有什么办法来隐藏谷歌照片应用而挑选只从本地设备的图像。或是谷歌的照片是 Intent.EXTRA_LOCAL_ONLY


解决方案

  

EXTRA_LOCAL_ONLY只告诉接收应用程序,它的回归
  唯一的数据是present。


Google+相册存储本地和远程的图片并因为注册用于额外的意图。但是显然它忽略的地方调用的意图有 EXTRA_LOCAL_ONLY 设置为true。

您可以尝试从列表中手动删除G +照片(然而,这似乎有点哈克):

 列表&LT;&意向GT;目标=新的ArrayList&LT;&意向GT;();
意向意图=新的Intent();
intent.setType(图像/ *);
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_LOCAL_ONLY,真);
清单&LT; ResolveInfo&GT;候选人= getPackageManager()queryIntentActivities(意向,0)。对于(ResolveInfo考生:考生){
  字符串的packageName = candidate.activityInfo.packageName;
  如果(packageName.equals(com.google.android.apps.photos)及!&放大器;!packageName.equals(com.google.android.apps.plus)及与放大器;!packageName.equals( com.android.documentsui)){
      意图iWantThis =新的Intent();
      iWantThis.setType(图像/ *);
      iWantThis.setAction(Intent.ACTION_GET_CONTENT);
      iWantThis.putExtra(Intent.EXTRA_LOCAL_ONLY,真);
      iWantThis.setPackage(的packageName);
      targets.add(iWantThis);
    }
}
意向选择器= Intent.createChooser(targets.remove(0),选择图片);
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS,targets.toArray(新Parcelable [targets.size()]));
startActivityForResult(选配,1);

解释几句: targets.remove(0)将删除,并从目标列表返回第一个意图,所以选择器将包括只有一个应用程序的。然后用 Intent.EXTRA_INITIAL_INTENTS 我们添加的其余部分。

在code段是从这款改装版的链接

请记得要检查所有条件一样,如果有至​​少一个可用的应用程序等。

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);

As per Intent.EXTRA_LOCAL_ONLY doesnt work

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

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.)

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

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.

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 only tells the receiving app that it should return only data that is present.

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.

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);

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.

The code snippet is modified version from this link.

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

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

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