2015-01-04 126 views
1

我在我的应用程序中试图做的是让用户从他手机的图库中选择一张图片(不想要get gallery images only,但也允许用户选择他们的应用程序选择)。我正在使用的代码如下:Android:为什么Intent.EXTRA_LOCAL_ONLY显示Google照片

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 doesnt work

EXTRA_LOCAL_ONLY只告诉接收应用程序,它应该返回 只存在数据。

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

我也试过Android image picker for local files only但它隐藏了所有具有远程图像的应用程序,不包括Google照片。

注意:所有图像路径是正确的,因为我做了Android Gallery on KitKat returns different Uri for Intent.ACTION_GET_CONTENT(感谢@Paul Burke),但我不想选择Internet /远程图像。

所以我的问题有没有什么办法可以隐藏谷歌照片应用程序,而只从本地设备选择图像。或者是谷歌的照片是Intent.EXTRA_LOCAL_ONLY

回答

6

部分EXTRA_LOCAL_ONLY只告诉接收应用程序,它应该回报 只存在数据。

Google+照片存储本地和远程图片,并因此为该意图注册了该额外内容。但显然它会忽略任何呼叫意图的地方EXTRA_LOCAL_ONLY设置为true。

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

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.EXTRA_INITIAL_INTENTS我们添加其余。

该代码片段从此link修改版本。

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

+0

感谢回答,但现在它显示了选择器对话框中的所有目标应用程序,并且在该对话框中显示了一个名为** document **的选项,它再次显示所有应用程序, **选择**文档**后,再次显示**谷歌照片**。 –

+0

我会检查我的代码,稍后再回复。 – Nuwisam

+0

当然,无论如何感谢帮助。 –